Continue applyForce when touch down

Hi!
I have an object that is supposed to move up (applyForce) when I touch the screen and continue to move up when I hold down the screen. And when touch ended the applyForce will stop.

This is what I’ve come up with:

function moveShooter(event)  
if ( event.phase == "began" ) then  
 shooter:applyForce( 0, -2, shooter.x, shooter.y )  
 elseif ( event.phase == "moved" ) then  
 Runtime:addEventListener("enterFrame", moveShooter2)  
 elseif ( event.phase == "ended" ) then  
 shooter:applyForce( 0, 0, shooter.x, shooter.y )  
end  
end  
  
function moveShooter2(event)  
 shooter:applyForce( 0, -0.05, shooter.x, shooter.y )  
end  
background:addEventListener("touch", moveShooter)  

If I press and just hold it just move up once, then go down (by gravity). But if I move around when holding down, the object goes up and disappears from display. What I want is that if I press and hold the object moves up until I stop holding, then it goes down.

How should I do this?

Best regards,
joelwe [import]uid: 54640 topic_id: 9715 reply_id: 309715[/import]

When you’re touching the screen and not moving it, this part (and only this) gets executed:
[lua]shooter:applyForce( 0, -2, shooter.x, shooter.y )[/lua]
That’s why it moves up only once.

And when you move your finger, EVERY move registers a new event listener so this:[lua]function moveShooter2(event)
shooter:applyForce( 0, -0.05, shooter.x, shooter.y )
end[/lua]gets executed a dozen times every frame. Not what you want.

Try this instead:[lua]function registerShooterMovement(event)
if ( event.phase == “began” ) then
Runtime:addEventListener(“enterFrame”, moveShooter)
elseif ( event.phase == “ended” ) then
Runtime:removeEventListener(“enterFrame”, moveShooter)
end
end

function moveShooter(event)
shooter:applyForce( 0, -0.05, shooter.x, shooter.y )
end

background:addEventListener(“touch”, registerShooterMovement)[/lua]

but I would do it differently for performance reason. I’m guessing this shooter is your main “character” in a shoot’em up game so I would recommend this:
[lua]shooter.vy = 0

local function moveShooter()
if shooter.vy < 0 then
shooter:applyForce( 0, shooter.vy, shooter.x, shooter.y )
end
end

local function onTouch(event)
if ( event.phase == “began” ) then
shooter.vy = -0.05
elseif ( event.phase == “ended” ) then
shooter.vy = 0
end
end

background:addEventListener(“touch”, onTouch)
Runtime:addEventListener(“enterFrame”, moveShooter)[/lua] [import]uid: 51516 topic_id: 9715 reply_id: 35393[/import]

Thank you very much seth.dev.ios!

At first, I didn’t see movement at all, but that was because the

shooter.vy = -0.05

needed to be changed to a higher value

shooter.vy = -1

Thank you again, sir! :smiley:

//joelwe [import]uid: 54640 topic_id: 9715 reply_id: 35396[/import]

Glad I could help. And it’s Seth, just Seth :wink: [import]uid: 51516 topic_id: 9715 reply_id: 35398[/import]

Nice explanation! [import]uid: 7472 topic_id: 9715 reply_id: 35674[/import]