If I touch both control arrow in my game, the character will stop moving

Hi,

I have right and left arrow in my game to control the main character, I realize that if i touch both at the same time OR touch left button before release the right arrow (while holding one arrow then press another arrow) the character will stop moving.

Below is the code that i used for the left touch event:

function touchleft (event) if event.phase == "began" then if gameIsActive == true then pencetKiri = true pencetKanan = false motionx = -speed motiony = 0 physics.removeBody( player ) local triangleShape = { -25,10, -25,-45, 18,10, 18,-45} physics.addBody(player, {shape=triangleShape,density = 0, friction = 0, bounce = 0}) player.bodyType = "kinematic" player:setSequence("playerleft") player:play("playerleft") end elseif event.phase == "moved" then -- do nothing else motionx = 0 motiony = 0 player:pause() end return true end left:addEventListener("touch", touchleft)

Right arrow event using the same code but different motionX.

What i want to achieve is the character should move to the last arrow he touched.

Let say when i touch the left arrow and after that i touch the right arrow without releasing the left arrow, the character should move to the right.

Please advise.

Thank You

Wouldn’t you just want to create the physics body for your player at the start of the game once and use your motionx, motiony to only move it and not inside your movement function because it looks like every time you let go and press it again it looks like you are always recreating a physics body which would be very unoptimized. Should also have it setting focus and move the “moved” phase stuff to an “ended” phase as it looks like you want everything to stop at that part.

Could also set up the movement function for the 4 buttons on just 1 function and it just checks which button you pressed and simply do your motion stuff and player:setSequence stuff to setup focus, etc?

^ This.  Have 4 event listeners (1 for each button of course) and have your movementDirection function check which one is being pressed and respond accordingly.

Wouldn’t you just want to create the physics body for your player at the start of the game once and use your motionx, motiony to only move it and not inside your movement function because it looks like every time you let go and press it again it looks like you are always recreating a physics body which would be very unoptimized. Should also have it setting focus and move the “moved” phase stuff to an “ended” phase as it looks like you want everything to stop at that part.

Could also set up the movement function for the 4 buttons on just 1 function and it just checks which button you pressed and simply do your motion stuff and player:setSequence stuff to setup focus, etc?

^ This.  Have 4 event listeners (1 for each button of course) and have your movementDirection function check which one is being pressed and respond accordingly.