Issue with event listener and applyForce

This is probably and easy one but i’m stumped.

I have 3 buttons to apply force to an object. They all work fine if you press and release the buttons, but if you press and then slide your finger off the button it won’t register the “ended” event phase. Here’s some code so you can see.

function registerUpMovement(event) if ( event.phase == "began" ) then print('ENGINE UP!') Runtime:addEventListener("enterFrame", upThrust) elseif ( event.phase == "ended" ) then print('RELEASED!') Runtime:removeEventListener("enterFrame", upThrust) end end function upThrust(event) jetpack:applyForce(0,-400, jetpack.x, jetpack.y) end upButton = display.newImage('images/arrowButton.png') upButton:addEventListener("touch",registerUpMovement) [import]uid: 79620 topic_id: 20730 reply_id: 320730[/import]

Hi there,
I can’t necessarily “write your code” for you, but the example you provide is somewhat inefficient and could be improved upon… and maybe your problem would go away too. :slight_smile:

First and foremost, you should create buttons with an individual variable (each) that “manages” which direction you want to thrust the jetpack. This way, you consolidate all of your button press actions into one local function which tells Corona what to do, depending on the button pressed.

For example…

local thrustDirection = ""  
  
local function doThrust() --this is a Runtime listener  
 if ( thrustDirection == "up" ) then  
 jetpack:applyForce(0,-400, jetpack.x, jetpack.y)  
 elseif ( thrustDirection == "left" ) then  
 --left thrust force here  
 elseif ( thrustDirection == "right" ) then  
 --right thrust force here  
 end  
end  
  
local function buttonThrust( event )  
 if ( event.phase == "began" ) then  
 thrustDirection = event.target.direction --"event.target.direction" is the button touched  
 Runtime:addEventListener("enterFrame", doThrust) --start Runtime listener  
 elseif ( event.phase == "ended" ) then  
 Runtime:removeEventListener("enterFrame", doThrust)  
 end  
end  
  
local upButton = display.newImage('images/arrowButton.png')  
upButton:addEventListener( "touch", buttonThrust )  
upButton.direction = "up" --specify which direction this button performs  

Also make everything (functions, variables, objects) local , I can’t emphasize this enough. Maybe your example was simply a basic illustration, but localizing everything in Lua is of utmost importance for overall performance.

Another huge problem you might be facing is that Corona can add MULTIPLE event listeners for the exact SAME event. For example, in your code, every time the user presses the “up” button, it creates another Runtime listener for that behavior. So, if you press that button 5 times, you’ll actually have FIVE duplicate Runtime listeners registering the same upward force movement, and unless you remove all five of them, you’ll still be getting upward force! I complained about this recently (which initiated a thorny argument in theory among some developers) and I requested that Ansca fixes it, but for the time being, you need to personally manage and ensure that you’re only creating ONE thrust listener at any current time. If you can’t figure out how to do this, let me know and I can give you some advice on the issue.

Best of luck,
Brent Sorrentino
Ignis Design
[import]uid: 9747 topic_id: 20730 reply_id: 81433[/import]