local playerMoveDirection = "stop"; local playerMoveSpeed = 2; local playerMoveTime = 2; local stopOnce = true; -- Function that moves the player local function movePlayer(player, moveX, moveY, moveTime) transition.moveBy(player, {x = moveX, y = moveY, time = moveTime}) end -- Setup player touch listener M.touchListener = function(event) if (event.phase == "began" or event.phase == "moved") then display.getCurrentStage():setFocus(event.target) if (event.x \< display.contentCenterX and playerMoveDirection ~= "down") then playerMoveDirection = "down" elseif (event.x \> display.contentCenterX and playerMoveDirection ~= "up") then playerMoveDirection = "up" end elseif (event.phase == "ended" and playerMoveDirection ~= "stop") then playerMoveDirection = "stop" display.getCurrentStage():setFocus(nil) end return true end local function checkPlayerMoveStatus() if (playerMoveDirection == "down") then stopOnce = true movePlayer(playerObject, 0, playerMoveSpeed, playerMoveTime) elseif (playerMoveDirection == "up") then stopOnce = true movePlayer(playerObject, 0, -(playerMoveSpeed), playerMoveTime) elseif (playerMoveDirection == "stop" and stopOnce == true) then stopOnce = false movePlayer(playerObject, 0, 0, 0) end end Runtime:addEventListener("enterFrame", checkPlayerMoveStatus)
When I place _movePlayer(playerObject, 0, playerMoveSpeed, playerMoveTime) _inside the touchListener function, the playerObject does in fact move as expected. But when I do it the way I have shown above, the playerObject does not move. Can anyone help me understand why it does not work this way or am I missing something?
I would suggest putting a print statement in your movePlayer() function to print out what parameters are being passed in. You can then do the same thing with the movePlayer in the touch handler and see what values are different.
I tried that, both ways produce the same print output.
When I put **movePlayer(playerObject, 0, playerMoveSpeed, playerMoveTime) **inside the touchListener function it seems to work only when I comment out Runtime:addEventListener(“enterFrame”, checkPlayerMoveStatus)
Is there something I need to declare before I can use Runtime?
I know you feel you have this solved, but be aware that if you’re using transition.* calls in short order, you should always cancel any prior calls on the object in question or you may have unwanted competing and overlapping calls in play:
local function movePlayer(player, moveX, moveY, moveTime) transition.cancel( player ) transition.moveBy(player, {x = moveX, y = moveY, time = moveTime}) end
I would suggest putting a print statement in your movePlayer() function to print out what parameters are being passed in. You can then do the same thing with the movePlayer in the touch handler and see what values are different.
I tried that, both ways produce the same print output.
When I put **movePlayer(playerObject, 0, playerMoveSpeed, playerMoveTime) **inside the touchListener function it seems to work only when I comment out Runtime:addEventListener(“enterFrame”, checkPlayerMoveStatus)
Is there something I need to declare before I can use Runtime?
I know you feel you have this solved, but be aware that if you’re using transition.* calls in short order, you should always cancel any prior calls on the object in question or you may have unwanted competing and overlapping calls in play:
local function movePlayer(player, moveX, moveY, moveTime) transition.cancel( player ) transition.moveBy(player, {x = moveX, y = moveY, time = moveTime}) end