簡體   English   中英

簡單的Corona SDK應用-觸摸事件

[英]Simple Corona SDK App - Touch Event

我是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是充當傳感器的空白圖片

如果要縮放圖片以始終完全適合屏幕的一半,請執行以下操作:

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

對藍色框重復上述步驟。 您還想做什么?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM