Hi there, I have this big issue, and im so pissed off because of this. I have no idea anymore how to make a function that handles touching more than one object. To introduce what i have and what should it do.
I have 3 objects, two buttons: LEFT and RIGHT. and SHOOTING button. I need to be able to shoot while holding the rotating button. I have this (code below) and it does work but if i touch shooting button it stops rotating.
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 )