Android 具有保持事件的多点触控检测
我有个大问题,为此我很生气。我再也不知道如何创建一个处理多个对象的函数。介绍我拥有的以及它应该做什么。 我有3个对象,两个按钮:左和右。还有射击按钮。我需要能够在按住旋转按钮的同时拍摄。我有这个(下面的代码),它确实可以工作,但如果我按下拍摄按钮,它就会停止旋转Android 具有保持事件的多点触控检测,android,lua,coronasdk,Android,Lua,Coronasdk,我有个大问题,为此我很生气。我再也不知道如何创建一个处理多个对象的函数。介绍我拥有的以及它应该做什么。 我有3个对象,两个按钮:左和右。还有射击按钮。我需要能够在按住旋转按钮的同时拍摄。我有这个(下面的代码),它确实可以工作,但如果我按下拍摄按钮,它就会停止旋转 local isShooting = false function resetShooting() isShooting = false end function holdingLeft() if isPressed
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 )
本地isShooting=false
函数resetShooting()
isShooting=false
结束
函数holdingLeft()
如果isPressed==true,则
如果rotationOfship>0,则
如果触摸X次<10,则
rotationOfship=rotationOfship-2
touchedXTimes=touchedXTimes+1
ship.rotation=船舶的旋转
否则触摸X次>9次
rotationOfship=rotationOfship-5
touchedXTimes=touchedXTimes+1
ship.rotation=船舶的旋转
如果触摸X次>29次
rotationOfship=rotationOfship-8
touchedXTimes=touchedXTimes+1
ship.rotation=船舶的旋转
结束
其他的
旋转船舶=360
结束
elseif isPressed==false然后
定时器。取消(tmr_保持)
结束
结束
函数holdingRight()
如果isPressed==true,则
如果旋转速度小于360,则
如果触摸X次<10,则
rotationOfship=rotationOfship+2
touchedXTimes=touchedXTimes+1
ship.rotation=船舶的旋转
否则触摸X次>9次
rotationOfship=rotationOfship+5
touchedXTimes=touchedXTimes+1
ship.rotation=船舶的旋转
如果触摸X次>29次
rotationOfship=rotationOfship+8
touchedXTimes=touchedXTimes+1
ship.rotation=船舶的旋转
结束
打印(触摸X次)
其他的
旋转速度=0
结束
elseif isPressed==false然后
定时器。取消(tmr_保持)
结束
结束
函数onTouch(事件)
如果event.target.name==“left”,则
如果event.phase==“开始”,则
isPressed=true
display.getCurrentStage():setFocus(event.target)
event.target.isFocus=true
如果tmr\U hold~=nil,则定时器取消(tmr\U hold)结束
tmr_hold=定时器。延时性能(8,保持左,0)
elseif event.target.isFocus然后
如果event.phase==“结束”或event.phase==“取消”,则
isPressed=false
定时器。取消(tmr_保持)
touchedXTimes=0
display.getCurrentStage():设置焦点(nil)
event.target.isFocus=false
结束
结束
结束
如果event.target.name==“right”,则
如果event.phase==“开始”,则
isPressed=true
display.getCurrentStage():setFocus(event.target)
event.target.isFocus=true
如果tmr\U hold~=nil,则定时器取消(tmr\U hold)结束
tmr_hold=定时器。性能带延迟(8,保持右侧,0)
elseif event.target.isFocus然后
如果event.phase==“结束”或event.phase==“取消”,则
isPressed=false
定时器。取消(tmr_保持)
touchedXTimes=0
display.getCurrentStage():设置焦点(nil)
event.target.isFocus=false
结束
结束
结束
如果event.target.name==“激光”,则
如果event.phase==“开始”且isShooting==false,则
isShooting=真
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=旋转HIP-90
addBody(项目符号,“动态”{isSensor=true,radius=10})
分组:插入(项目符号)
船舶:toFront()
播放(激光声音,{channel=2,loops=0})
本地newX,newY=点距离(旋转HIP-90400)
项目符号:setLinearVelocity(新X/2、新Y/2)
tmr_shoot=定时器性能带延迟(数学随机(300400),重置shooting,1)
elseif event.target.isFocus然后
如果event.phase==“结束”或event.phase==“取消”,则
isShooting=false
计时器。取消(tmr_拍摄)
display.getCurrentStage():设置焦点(nil)
event.target.isFocus=false
结束
结束
结束
返回真值
结束
dpad_左_圆:添加文本列表(“触摸”,onTouch)
dpad_right_circle:addEventListener(“触摸”,onTouch)
激光按钮圆圈:添加显示器(“触摸”,onTouch)
我建议将其分为三种方法,而不是一种:
dpad_left_circle:addEventListener( "touch", onTouchLeft )
dpad_right_circle:addEventListener( "touch", onTouchRight )
laser_button_circle:addEventListener( "touch", onTouchLaser )
代码将更加清晰,您可以同时触摸不同的按钮