Unable to get animation to play on FlxSprite

157 views
Skip to first unread message

Ted Molinski

unread,
May 12, 2014, 3:26:40 PM5/12/14
to haxef...@googlegroups.com
Hi everyone, I was searching but unable to find an topic that was related to this one.

I'm trying to create an animated sprite. I have it working when I create a new FlxSprite and add all the animations then play the animation(look at MenuState lines 21-26). However I am unable to get the animations to work when I create a new Hero object that extends the FlxSprite class. I originally wanted all the animation code in the constructor of the Hero object but I've been just experimenting trying to get it to happen at all (MenuState lines 9-13). I was able to specify which frame index I want and it changed to the correct frame index. It just seems that play won't actually start the process. Does anyone know what might be the issue?

  5 class MenuState extends FlxState
  6 {      
  7         override public function create():Void
  8         {
  9                 var hero = new Hero(100, 100);
 10                 hero.loadGraphic("images/manwalkdown.png", true, 33, 86);
 11                 hero.animation.add("test", [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 10, true);
 12                 add(hero);
 13                 hero.animation.play("test");
 14                
 15                 var upplayer : FlxSprite = new FlxSprite();
 16                 upplayer.loadGraphic("images/manwalkup.png", true, 31, 85);
 17                 upplayer.animation.add("up", [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 10, true);
 18                 upplayer.animation.play("up");
 19                 add(upplayer);
 20        
 21                 var downplayer : FlxSprite = new FlxSprite();
 22                 downplayer.loadGraphic("images/manwalkdown.png", true, 33, 86);
 23                 downplayer.animation.add("down", [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 10, true);
 24                 downplayer.x = 200;
 25                 add(downplayer);
 26                 downplayer.animation.play("down");
 27                                
 28                 super.create();
 29         } 


  5 class Hero extends FlxSprite {
  6         public var currentMesh : NavMesh;                       
  7         public var targets : Array<Vector2> = new Array<Vector2>();
  8         public var targetMeshes : Array<NavMesh> = new Array<NavMesh>();
  9                
 10         public function new(newX : Int, newY : Int) {
 11                 super(newX, newY);
 12                

Gama11

unread,
May 12, 2014, 3:36:50 PM5/12/14
to
This works for me:

package;

import flixel.FlxSprite;
import flixel.FlxState;
using flixel.util.FlxSpriteUtil;

class PlayState extends FlxState
{
override public function create():Void
{
add(new Player());
}
}

class Player extends FlxSprite
{
public function new()
{
super();
loadGraphic("assets/images/spaceman.png", true, 8, 8);
animation.add("animation", [0, 1]);
animation.play("animation");
}
}

Ted Molinski

unread,
May 12, 2014, 3:43:01 PM5/12/14
to haxef...@googlegroups.com
Thanks for the quick response but I don't understand why yours works and mine does not. Could you please attach the spaceman image so that I could try to see if it's something wrong with the image?

Gama11

unread,
May 12, 2014, 3:45:24 PM5/12/14
to haxef...@googlegroups.com
This is the graphic.

Ted Molinski

unread,
May 12, 2014, 3:47:35 PM5/12/14
to haxef...@googlegroups.com
I just discovered something strange. When I attached my extended FlxSprite object in the same file it worked but once I took it out into it's own file it stopped again. Could you try your code but splitting the two classes apart?

Ted Molinski

unread,
May 12, 2014, 3:52:54 PM5/12/14
to haxef...@googlegroups.com
Ugh... I found it. My override update function was missing the super call. Thanks for the help Gama11
Reply all
Reply to author
Forward
0 new messages