简单的Corona SDK应用程序 - 触摸事件

时间:2013-12-30 10:28:37

标签: lua corona

我是Lua的新手,目前正在开发名为Red VS Blue的项目。它主要针对两支球队,红队和蓝队,并且它作为记分板。如果我点击左侧,红色得到1分。如果我点击蓝色边,蓝色会得到一个点。屏幕分辨率为320 x 480,但根据设备而变宽。 这是一个截图: http://i40.tinypic.com/5lqosk.jpg

这是我的代码:

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是充当传感器的空白图片

1 个答案:

答案 0 :(得分:0)

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

--[[
    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 )

重复蓝色方框。还有其他你想做的事吗?