Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am trying to animate a FlxSprite, whose image I loaded from an embedded sprite sheet. I instantiate the sprite like this:

// embedding a png file given a relative path
[Embed(source="../Images/BatGraphic.png")] public var Bat_Enemy:Class;
override public function create():void
{                       
//instantiate bat sprite
bat = new FlxSprite(FlxG.width/2-5,FlxG.width/2);
bat.loadGraphic(Bat_Enemy,true,false,30,12,false);

//add animation to bat
var frames:Array = new Array(12);
AddAnimationToSprite(bat,frames,"batAnimation",30);
//add bat enemy to screen
add(bat);

I use this function to add the animation to the sprite, it essentially just calls the Flixel API:

public function AddAnimationToSprite(sprite:FlxSprite, frames:Array,animName:String,frameRate:int):void
{
   var animation:FlxAnim = new FlxAnim(animName,frames,frameRate,true);
   sprite.addAnimation(animName,frames,frameRate,true);

}

Then, in the update function, I call the FlxSprite play method to animate the sprite:

public function update:void()
{
/*animate relevant sprites*/
bat.play("batAnimation",true);
/*end animation*/

/*update screen based on player position*/
super.update();
}

Yet, the sprite does not animate. Is there a step I am missing? How do you use FlxAnim to animate a FlxSprite?

Update: I altered the code so that the FlxAnim.play() is called in the create as suggested as follows:

//instantiate bat sprite
bat = new FlxSprite(FlxG.width/2-5,FlxG.width/2);
bat.loadGraphic(Bat_Enemy,true,false,30,12,false);

//add animation to bat
var frames:Array = new Array(12);
//AddAnimationToSprite(bat,frames,"batAnimation",30);
var batAnim:FlxAnim = new FlxAnim("batAnim",frames,30,true);

add(bat);
bat.play("batAnim");

However, still nothing happens. Potentially, I don't have the right number of frames, since I only wanted to use the first row of sprites in the sheet to animate. But even if the number of frames were off, it should still be animating in some fashion right? Like flickering between two sprites? What am I missing here? Is 30 the appropriate fps for Flixel animations?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Simple, you're calling bat.play("batAnimation",true); in your update loop. That means, your animation will restart every frame, resulting in a still image.

The second parameter to the play function is a flag whether or not the animation should be forced to restart (see docs). So either call:

bat.play("batAnimation", false);

in your update loop, or what I would consider a better approach is to call bat.play("batAnimation",true); somewhere outside the update method. For example here:

...
//add bat enemy to screen
add(bat);
bat.play("batAnimation");
share|improve this answer
1  
Incidentally I made exactly the same mistake recently with FlashPunk. API designers, please don't overload functions with extra boolean parameters - add a new function name, eg PlayFromStart! –  Kylotan Dec 29 '12 at 10:38

You're not filling your array with anything. It has to contain numbers for the FlxSprite class to understand it. Otherwise you're sending in an array of size 12 filled with all 0's.

share|improve this answer
    
Welcome to GD.SE! Although this answer is perfectly fine, the question is over 6 months old - it's typically better to answer newer questions. –  Polar Jul 12 '13 at 10:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.