Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua 简单电晕SDK应用程序-触摸事件_Lua_Coronasdk - Fatal编程技术网

Lua 简单电晕SDK应用程序-触摸事件

Lua 简单电晕SDK应用程序-触摸事件,lua,coronasdk,Lua,Coronasdk,我是Lua的新手,目前正在从事一个名为“红色VS蓝色”的项目。它基本上是两支球队的,红队和蓝队,它的功能是记分牌。如果我点击左侧,红色得1分。如果我点击蓝色的一面,蓝色得到一分。屏幕分辨率为320 x 480,但会根据设备的不同而变宽。 以下是一个屏幕截图: 这是我的代码: display.setStatusBar(display.HiddenStatusBar); local redscore = 0 local bluescore = 0 local background = displa

我是Lua的新手,目前正在从事一个名为“红色VS蓝色”的项目。它基本上是两支球队的,红队和蓝队,它的功能是记分牌。如果我点击左侧,红色得1分。如果我点击蓝色的一面,蓝色得到一分。屏幕分辨率为320 x 480,但会根据设备的不同而变宽。 以下是一个屏幕截图:

这是我的代码:

display.setStatusBar(display.HiddenStatusBar);
local redscore = 0
local bluescore = 0
local background = display.newImage("images/background.png");
local redc = display.newImage("images/redc.png", 0, 0);
local bluec = display.newImage("images/bluec.png", 240, 0);
local redtext = display.newText(redscore, 55, 100, native.systemFont, 32*4);
local bluetext = display.newText(bluescore , 290, 100, native.systemFont, 32*4);

local function redt( event )
    redscore = redscore + 1;
    return true
end
redc:addEventListener( "tap", redt )

local function bluet( event )
    bluescore = bluescore + 1;
    return true
end
bluec:addEventListener( "tap", bluet )

redc和bluec是用作传感器的空白图片

如果您希望缩放图片以始终完全适合屏幕的一半,请执行以下操作:

--[[
    Take half the width of the device and adjust for larger screens, dividing by
    the current width of the picture to produce the value it needs to be scaled
    by to fit
]]
local wScale = ((display.layoutWidth/2) - display.screenOriginX) / redc.contentWidth

--Do the same for the height
local hScale = ((display.layoutHeight/2) - display.screenOriginY) / redc.contentHeight

--Scale the image
redc:scale(wScale,hScale)

--[[
    Position the image. Since the default is a central reference point, you take half of              
    the image's width/height and add the adjustment for larger screens
]]
redc.x = (redc.contentWidth/2) + display.screenOriginX
redc.y = (redc.contentHeight/2) + display.screenOriginY
要在触摸框时更新记分板,请按如下方式构造事件侦听器:

local function redt( event )
    redscore = redscore + 1;
    redtext.text = tostring(redscore)
    return true
end
redc:addEventListener( "tap", redt )

对蓝色框重复上述步骤。您还想用它做什么?

您的问题是什么?嗯,触摸功能不起作用!非常感谢你的回答!现在可以了。这只是事后的想法,通常当你编写lua或任何其他语言的代码时,你会发表评论,还是只是为了教我?像这样:--这是一个注释没有问题,Corona有时会有点问题-我没有骗你,我已经解决了一些“this object is nil”错误,只需使用
print(tostring(object))
打印出对象的字符串表示。直到今天,我都很困惑。注释代码始终是最佳实践,首先是因为如果您稍后再来,有时可能会忘记您所做的变通方法或快速修复。其次,因为每个人都以不同的方式编写代码,因此其他人在没有注释的情况下查看/帮助编辑您的代码将非常困难注:在lua中,这是一条单行注释[[这是一条多行注释]],正如您从我上面的回答中所看到的