Author Topic: Problems starting an animation at a random frame  (Read 1390 times)

Noid.EXE

  • Member
  • **
  • Posts: 11
  • Karma: +0/-0
    • View Profile
    • NoidEXE on deviantART
I initialize a sprite and set its current frame to random (using randomframe()). However, the first play() in the update() loop resets the animation to frame 0.

The problem seems to be that randomframe() sets _curAnim to null. If play() finds that _curAnim is null it restarts the animation instead of advancing to the next frame. However, changing the behavior of randomframe() doesn't seem to help.

So the question is: How do I initalize a sprite with an animation starting at a random frame?

Code: [Select]
package 
{
/**
* ...
* @author Noid
*/
import org.flixel.*;

public class Soul extends FlxSprite
{
[Embed(source = '../assets/soul2.png')] private var ImgSoul:Class;
private var IsRandomized:Boolean = false;

public function Soul(X:Number = 0, Y:Number = 0)
{
super (X, Y);
loadGraphic(ImgSoul, true, false, 15, 15);
addAnimation("Idle", [0, 1, 2, 3, 4, 5, 6], 15, true);
play("Idle"); //Set the current animation to play
//frame = 3;
randomFrame(); //Set animation to a random frame
trace(frame);

}

override public function update():void
{
play("Idle");
super.update();
trace(frame);

}

/*
  override public function kill()
  {
  }
*/


}

}

initials

  • Contributor
  • ****
  • Posts: 378
  • Karma: +0/-0
  • Initials
    • View Profile
    • Initials Blog. Code and other things.
Re: Problems starting an animation at a random frame
« Reply #1 on: Sun, Apr 17, 2011 »
A few things I've noticed here, and I'm not totally up to date with v2.5.
What version are you using?

Anyway, first of all, instead of

Code: [Select]
trace(frame);
try

Code: [Select]
FlxG.log(_curFrame)
I've noticed in 2.5 FlxG.log doesn't go to the FlashDevelop console, only to the Flixel debug window (press ~ to see it).

Also you are calling play(Idle) in your update function. So it will play it with every update. You shouldn't need to do this. Calling play in your constructor is enough to set it off.
Not sure if these will fix your problems, but try it out.
Initials: Super Lemonade Factory, Super Lemonade Factory Part Two, Above The Clouds, Revvolvver, Four Chambers of the Human Heart

Noid.EXE

  • Member
  • **
  • Posts: 11
  • Karma: +0/-0
    • View Profile
    • NoidEXE on deviantART
Re: Problems starting an animation at a random frame
« Reply #2 on: Sun, Apr 17, 2011 »
Thanks for your help, it helped me clean a few things.

I finally got it to work using this solution http://flixel.org/forums/index.php?topic=1902.0

Using v2.35 BTW