[英]Multitouch detection with holding event Corona SDK
我有這個大問題,因此我很生氣。 我不知道如何制作一個處理觸摸多個對象的功能。 介紹我有什么,應該做什么。 我有3個對象,兩個按鈕:向左和向右。 和拍攝按鈕。 我需要能夠在按住旋轉按鈕的同時進行拍攝。 我有這個(下面的代碼),它確實可以工作,但是如果我觸摸拍攝按鈕,它就會停止旋轉。
local isShooting = false
function resetShooting()
isShooting = false
end
function holdingLeft()
if isPressed == true then
if rotationOfship > 0 then
if touchedXTimes < 10 then
rotationOfship = rotationOfship-2
touchedXTimes = touchedXTimes + 1
ship.rotation = rotationOfship
elseif touchedXTimes > 9 then
rotationOfship = rotationOfship-5
touchedXTimes = touchedXTimes + 1
ship.rotation = rotationOfship
elseif touchedXTimes > 29 then
rotationOfship = rotationOfship-8
touchedXTimes = touchedXTimes + 1
ship.rotation = rotationOfship
end
else
rotationOfship = 360
end
elseif isPressed == false then
timer.cancel(tmr_hold)
end
end
function holdingRight()
if isPressed == true then
if rotationOfship < 360 then
if touchedXTimes < 10 then
rotationOfship = rotationOfship+2
touchedXTimes = touchedXTimes + 1
ship.rotation = rotationOfship
elseif touchedXTimes > 9 then
rotationOfship = rotationOfship+5
touchedXTimes = touchedXTimes + 1
ship.rotation = rotationOfship
elseif touchedXTimes > 29 then
rotationOfship = rotationOfship+8
touchedXTimes = touchedXTimes + 1
ship.rotation = rotationOfship
end
print(touchedXTimes)
else
rotationOfship = 0
end
elseif isPressed == false then
timer.cancel(tmr_hold)
end
end
function onTouch(event)
if event.target.name == "left" then
if event.phase == "began" then
isPressed = true
display.getCurrentStage():setFocus( event.target )
event.target.isFocus = true
if tmr_hold ~= nil then timer.cancel(tmr_hold) end
tmr_hold = timer.performWithDelay( 8, holdingLeft, 0)
elseif event.target.isFocus then
if event.phase == "ended" or event.phase == "cancelled" then
isPressed = false
timer.cancel(tmr_hold)
touchedXTimes = 0
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
end
end
end
if event.target.name == "right" then
if event.phase == "began" then
isPressed = true
display.getCurrentStage():setFocus( event.target )
event.target.isFocus = true
if tmr_hold ~= nil then timer.cancel(tmr_hold) end
tmr_hold = timer.performWithDelay( 8, holdingRight, 0)
elseif event.target.isFocus then
if event.phase == "ended" or event.phase == "cancelled" then
isPressed = false
timer.cancel(tmr_hold)
touchedXTimes = 0
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
end
end
end
if event.target.name == "laser" then
if event.phase == "began" and isShooting == false then
isShooting = true
display.getCurrentStage():setFocus( event.target )
event.target.isFocus = true
bullet = display.newImageRect("images/laser.png",math.random(5,20),5/2)
bullet.x = halfW
bullet.y = halfH
bullet.name = "bullet"
bullet.rotation = rotationOfship-90
physics.addBody( bullet, "dynamic", { isSensor=true, radius=10} )
group:insert(bullet)
ship:toFront()
audio.play( laserSound, { channel=2, loops=0} )
local newX, newY = pointAtDistance(rotationOfship-90, 400)
bullet:setLinearVelocity( newX/2, newY/2 )
tmr_shoot = timer.performWithDelay( math.random(300,400), resetShooting, 1)
elseif event.target.isFocus then
if event.phase == "ended" or event.phase == "cancelled" then
isShooting = false
timer.cancel(tmr_shoot)
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
end
end
end
return true
end
dpad_left_circle:addEventListener( "touch", onTouch )
dpad_right_circle:addEventListener( "touch", onTouch )
laser_button_circle:addEventListener( "touch", onTouch )
我建議將其分為三種方法,而不是一種:
dpad_left_circle:addEventListener( "touch", onTouchLeft )
dpad_right_circle:addEventListener( "touch", onTouchRight )
laser_button_circle:addEventListener( "touch", onTouchLaser )
代碼將更加清晰,您將可以同時觸摸不同的按鈕。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.