Trouble getting platform movement working properly.

Edit: I fixed the part where the button lost focus, however I’m still unable to perform a jump while my “ball” is moving.

I’m new to programming and Corona in general and am having trouble getting this to move my “ball” properly. It works fine at first glance, however if you hold the button and drag off of it, then release, it won’t remove the Event Listener for my movement, therefore it will continue to move my player even after no button is being held.

Also – I tried this on my kindle fire and I was unable to move my player while simultaneously jumping… Any quick fix to this or some insight to help me understand the event system?

--Activate Physics & Gravity  
local physics = require "physics"  
  
physics.start()  
physics.setGravity(0,9.8)  
  
--Draw Mode  
physics.setDrawMode( "hybrid" )  
--Activate Multitouch  
system.activate( "multitouch" )  
  
--Show the ball  
local ball = display.newImage("ball.png")  
ball.x = display.contentWidth / 2  
ball.y = display.contentHeight / 2  
physics.addBody(ball, {density = 1.0, friction = 1, bounce = 0, radius = 18})  
  
--Show the Ground  
local ground = display.newRect(0,display.contentHeight -30 , 1000, 25)  
ground:setFillColor ( 255, 255, 255 )  
physics.addBody(ground, "static", {density = 1.0, friction = 1, bounce = 0})  
  
--Show the box  
local box = display.newRect(display.contentWidth/2,display.contentHeight - 65 , 32, 32)  
box:setFillColor ( 255, 255, 255 )  
physics.addBody(box, "static", {density = 1.0, friction = 1, bounce = 0})  
  
--Show the movement buttons  
local leftButton = display.newImage("leftButton.png")  
leftButton.x = display.contentWidth / 10  
leftButton.y = display.contentHeight - 85  
  
local rightButton = display.newImage("rightButton.png")  
rightButton.x = display.contentWidth / 4  
rightButton.y = display.contentHeight - 85  
  
local jumpButton = display.newImage("jumpButton.png")  
jumpButton.x = display.contentWidth - 75  
jumpButton.y = display.contentHeight - 85  
  
-- Movement Functions  
function moveLeft(event)  
 ball.x = ball.x - 3  
end  
  
function moveRight(event)  
 ball.x = ball.x + 3  
end  
  
--Movement (Revised)  
function leftButton:touch(event)  
 if (event.phase == "began") then  
 Runtime:addEventListener("enterFrame", moveLeft)  
 display.getCurrentStage():setFocus(event.target, event.id);  
 elseif (event.phase == "ended") then  
 Runtime:removeEventListener("enterFrame", moveLeft)  
 display.getCurrentStage():setFocus(event.target, nil);  
 end  
end  
  
function rightButton:touch(event)  
 if (event.phase == "began") then  
 Runtime:addEventListener("enterFrame", moveRight)  
 display.getCurrentStage():setFocus(event.target, event.id);  
 elseif (event.phase == "ended") then  
 Runtime:removeEventListener("enterFrame", moveRight)  
 display.getCurrentStage():setFocus(event.target, nil);  
 end  
end  
  
function jumpButton:tap(event)  
ball:applyForce(0, -600, ball.x, ball.y)  
end  
  
--Event Listeners  
leftButton:addEventListener("touch", leftButton)  
rightButton:addEventListener("touch", rightButton)  
jumpButton:addEventListener("tap", jumpButton)  

Thanks so much for taking the time to read my post. [import]uid: 131400 topic_id: 23035 reply_id: 323035[/import]

Can you add in an elseif event.phase == “moved” and do the same as you would in ended phase and see if that helps and then let me know, please?

Peach :slight_smile: [import]uid: 52491 topic_id: 23035 reply_id: 92131[/import]