--Hide status bar from the beginning display.setStatusBar( display.HiddenStatusBar ) --Variables to set characters X and Y cords to the center of the screen local centerXcord = display.contentCenterX local centerYcord = display.contentCenterY --Display character character = display.newImage( "images/character.png" ) --Move character to center of screen character.x = centerXcord character.y = centerYcord -- Add left joystick button local left = display.newImage ("images/left.png") left.x = 15; left.y = 270; -- Add right joystick button local right = display.newImage ("images/right.png") right.x = 65; right.y = 270; -- Add up joystick button local up = display.newImage ("images/up.png") up.x = 40; up.y = 245; -- Add down joystick button local down = display.newImage ("images/down.png") down.x = 40; down.y = 295; -- Variable used to move character along y axis motiony = 0; -- Variable used to move character along x axis motionx = 0; -- Walk Speed speed = 5; -- When left arrow is touched, move character left function left:touch() motionx = -speed; end left:addEventListener("touch",left) -- When right arrow is touched, move character right function right:touch() motionx = speed; end right:addEventListener("touch",right) -- When up arrow is touched, move character up function up:touch() motiony = -speed; end up:addEventListener("touch",up) -- When down arrow is touched, move character down function down:touch() motiony = speed; end down:addEventListener("touch",down) -- Move character local function movecharacter (event) character.x = character.x + motionx; character.y = character.y + motiony; end Runtime:addEventListener("enterFrame", movecharacter) -- Stop character movement when no arrow is pushed local function stop (event) if event.phase =="ended" then motionx = 0; motiony = 0; end end Runtime:addEventListener("touch", stop )
Im having a small issue with the code i made above. If i click the left arrow and hold down and hover over the up or down arrow my character moves as if both x and y motion is being applied. How can i stop this from happening? I want only 1 arrow to have effect at a time if that makes sense.