Sliding Sprite and No Animation

I have a small problem where when I touch my buttons to make my character move. If you touch them really quickly, the sprite slides to in the button’s direction without animating. :confused:

[lua]function createPiggyBank()
local piggyBankSheet = sprite.newSpriteSheet(“images/game_piggybank.png”, 96, 96)
local piggyBankSet = sprite.newSpriteSet(piggyBankSheet, 1, 6)
sprite.add (piggyBankSet, “pigmleft”, 1, 3, 250, 0)
sprite.add (piggyBankSet, “pigmright”, 4, 3, 250, 0)
piggyBank = sprite.newSprite(piggyBankSet)
physics.addBody(piggyBank, “static”)
piggyBank.x = display.contentWidth / 2
piggyBank.y = 397
function piggyBank:collision(event)
if(event.other.type == “coinbronze”) then
scorePlaceThree = scorePlaceThree + 1
audio.play(coinSound)
elseif(event.other.type == “coinsilver”) then
scorePlaceThree = scorePlaceThree + 5
audio.play(coinSound)
elseif(event.other.type == “coingold”) then
scorePlaceTwo = scorePlaceTwo + 1
audio.play(coinSound)
elseif(event.other.type == “rock”) then

end
event.other:removeSelf()
end
piggyBank:addEventListener(“collision”, piggyBank)
end

function createButtons()
local motionX = 0
local speed = 8
local buttonSheet = sprite.newSpriteSheet(“images/game_buttons.png”, 72, 72)
local buttonSet = sprite.newSpriteSet(buttonSheet, 1, 4)
sprite.add (buttonSet, “leftn”, 1, 1, 60000, 0)
sprite.add (buttonSet, “leftp”, 2, 1, 60000, 0)
sprite.add (buttonSet, “rightn”, 3, 1, 60000, 0)
sprite.add (buttonSet, “rightp”, 4, 1, 60000, 0)
leftButton = sprite.newSprite(buttonSet)
leftButton.x = 51
leftButton.y = 429
leftButton:prepare(“leftn”)
leftButton:play(“leftn”)
function touchLeft (event)
motionX = -speed
leftButton:prepare(“leftp”)
leftButton:play(“leftp”)
piggyBank:prepare(“pigmleft”)
piggyBank:play(“pigmleft”)
end
rightButton = sprite.newSprite(buttonSet)
rightButton.x = 749
rightButton.y = 429
rightButton:prepare(“rightn”)
rightButton:play(“rightn”)
function touchRight (event)
motionX = speed
rightButton:prepare(“rightp”)
rightButton:play(“rightp”)
piggyBank:prepare(“pigmright”)
piggyBank:play(“pigmright”)
end
local function movePiggyBank(event)
piggyBank.x = piggyBank.x + motionX
if(piggyBank.x < -33) then piggyBank.x = 833
elseif(piggyBank.x > 833) then piggyBank.x = -33 end
end
local function stopPiggyBank (event)
if event.phase ==“ended” then
motionX = 0
leftButton:prepare(“leftn”)
leftButton:play(“leftn”)
rightButton:prepare(“rightn”)
rightButton:play(“rightn”)
piggyBank:pause()
end
end
leftButton:addEventListener(“touch”, touchLeft)
rightButton:addEventListener(“touch”, touchRight)
Runtime:addEventListener(“enterFrame”, movePiggyBank)
Runtime:addEventListener(“touch”, stopPiggyBank)
end[/lua] [import]uid: 103624 topic_id: 18229 reply_id: 318229[/import]

How about adding some print() statements into your program?
This will show you when certain lines have been reached, so you can do some debugging. E.g. after line 66, insert: print('stopPiggyBank happened, phase is '…event.phase)
Repeat this for all your event-related functions.

What I suspect is happening is that stopPiggyBank is being called immediately after touchRight/touchLeft, because the touch is being picked up by the left/right button and also by Runtime. You might want to only add the listener to stop the piggy bank if the piggy bank is moving, and remove the listener when the piggy bank is not moving… as opposed to the way it is right now where the stop listener is running all the time. [import]uid: 70391 topic_id: 18229 reply_id: 69803[/import]

I narrowed the problem down to two causes:

  1. The sprite’s animation speed is 250, so I have to press that long for something to happen.

  2. stopPiggyBank() is being called immediately after I stop touching. [import]uid: 103624 topic_id: 18229 reply_id: 69816[/import]

Anyone know a solution to this problem? [import]uid: 103624 topic_id: 18229 reply_id: 69878[/import]

? [import]uid: 103624 topic_id: 18229 reply_id: 69953[/import]

Does anyone know how to fix this? [import]uid: 103624 topic_id: 18229 reply_id: 70034[/import]

Anyone have any way to fix this? [import]uid: 103624 topic_id: 18229 reply_id: 70104[/import]