Change Sprite Animation Based on Movement

Basically what i’m trying to do, is when the character is moving left or right, i want to play the “Left” and “Right” animation that i have created in my sprite sheet, the problem is when i try to call the animation, it only play 1 frame, this is what i have:

Set Animation Based on Speed

local function setHeroAnimation() &nbsp; local vx, vy = dan:getLinearVelocity() &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if vx \< 5 then -- if x velocity less than 5 play the standing animation &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hero:setSequence( "Parado" ) &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;else&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hero:setSequence( "Run" ) -- x velocity higher than 5 play the run animation &nbsp; &nbsp; &nbsp;end end &nbsp;

Spritesheet configuration

local myImageSheet = graphics.newImageSheet( "run.png", &nbsp;sheetInfo:getSheet() ) local sequenceData = {{ name = "Run", frames = {6,7,8,9,10,11,12,13,14,15,16,17} , time = 800},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { name = "Parado", frames = {1,2,3,4,5} , time = 900}} &nbsp;

This is the hero:
 

hero = display.newSprite( myImageSheet, sequenceData ) hero.x = 90 &nbsp;&nbsp;&nbsp;&nbsp;hero.y = 210 &nbsp;&nbsp;&nbsp;&nbsp;hero.isFixedRotation = true &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;physics.addBody( hero, "dynamic", physicsData:get("hero") )&nbsp; &nbsp; hero:play()&nbsp; &nbsp;

enterFrame Listener

Runtime:addEventListener( "enterFrame", setHeroAnimation )&nbsp;&nbsp; &nbsp;

  

When i move the character, the image changes for the first frame of the correct animation and stop, what i’m doing wrong?

I doesn’t play because you never tell it to play :P.

When you call play() after physics:addBody() then animation plays once just alright (it should be ‘Run’ because it’s first).

However you do not play it any more. In setHeroAnimation() you just change sequence and nothing more. Just add dan:play() after setSequence() (you should declare animation sequence as looping - see docs, I do not know what ‘dan’ is however.)

Dan is the Hero, i changed before posting, because i tought it would be easier to others understand, and i forgot to change in the function.

Anyway, i changed the function as it follows:

&nbsp; local vx, vy = dan:getLinearVelocity() &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if vx \< 5 then &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print( "Is idle" ) &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dan:setSequence( "Parado" ) &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dan:play() &nbsp; &nbsp; &nbsp;else&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("Is Moving") &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dan:setSequence( "Run" ) &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dan:play() &nbsp; &nbsp; &nbsp;end end &nbsp;

I getting the Print’s correctly but the problem persist.

Ah, maybe because you have it as runtime function? You tell animation to play but you do it on every frame so it tries to do it from first frame every time and it seems as it’s not playing…

I doesn’t play because you never tell it to play :P.

When you call play() after physics:addBody() then animation plays once just alright (it should be ‘Run’ because it’s first).

However you do not play it any more. In setHeroAnimation() you just change sequence and nothing more. Just add dan:play() after setSequence() (you should declare animation sequence as looping - see docs, I do not know what ‘dan’ is however.)

Dan is the Hero, i changed before posting, because i tought it would be easier to others understand, and i forgot to change in the function.

Anyway, i changed the function as it follows:

&nbsp; local vx, vy = dan:getLinearVelocity() &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if vx \< 5 then &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print( "Is idle" ) &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dan:setSequence( "Parado" ) &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dan:play() &nbsp; &nbsp; &nbsp;else&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("Is Moving") &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dan:setSequence( "Run" ) &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dan:play() &nbsp; &nbsp; &nbsp;end end &nbsp;

I getting the Print’s correctly but the problem persist.

Ah, maybe because you have it as runtime function? You tell animation to play but you do it on every frame so it tries to do it from first frame every time and it seems as it’s not playing…