Change animation based on movement direction?

I’m looking for a way to change the current animation based on the direction the player is moving. What I’ve done so far is use sprite.newSpriteSheet and sprite.newSpriteSet to create animations for each direction as follows:

local frontwalkAnim = sprite.newSpriteSheet("front-walkanim.png", 32, 32);  
local frontSet = sprite.newSpriteSet( frontwalkAnim, 1, 3 );  
sprite.add( frontSet, "frontwalk", 1, 3, 1000, -2 );  

Then with this I’ll create the initial player icon using the following:

local playericon = sprite.newSprite( frontSet );  
playericon.x = 72;  
playericon.y = 151;  
playericon:prepare("frontwalk");  
physics.addBody( playericon, "dynamic", { density=2.0, friction=0.5, bounce=0.2, radius=16 } )  

The part I’m getting stuck on is how to change the sprite animation for playericon to either backSet, rightSet, or leftSet depending on the direction the player is moving. [import]uid: 38084 topic_id: 6693 reply_id: 306693[/import]

You obviously know about the prepare() command since you use it to start the initial animation, so I’m not sure what you’re confused about. Call prepare(“backwalk”) when they’re walking backwards.

ADDITION: oh wait, from looking at your code it looks like you only have the frames for “frontwalk” on your spritesheet. That’s not the way to do it. What you want is to have all your animations on one spritesheet, and then use sprite.add()to split it up.

Or, I guess you could have 4 different sprites and swap between them as the player moves, but that seems like more work than is necessary. [import]uid: 12108 topic_id: 6693 reply_id: 23510[/import]