Sprite animation end with wrong frame?



  • First of all,im not good at English :( sorry.
    so here's the thing. im following the official tutorial step by step. but today I got a problem.
    in this section: 4 - Sprites and Animation,i try to add animation for my sprite, everything is going well, the program is compiled successfully.but the result are slightly different from expect.

    The author of this article says "we want each animation to end with the player in their 'neutral' pose (legs together), that way each time we animate the player, it will return to the correct frame as soon as they stop animating. "
    But from my results,the animation end with the last frame(not go back to the first frame of this animation),is that a bug? or I have some misunderstand?

    here is my code:

    package;
    
    import flixel.FlxSprite;
    import flixel.system.FlxAssets.FlxGraphicAsset;
    import flixel.util.FlxColor;
    import flixel.FlxG;
    import flixel.math.FlxPoint;
    import flixel.FlxObject;
    
    class Player extends FlxSprite
    {
    
        var mA:Float;
        var speed:Float = 200;
        public function new(?X:Float=0, ?Y:Float=0)
        {
            super(X,Y);
            drag.x = drag.y = 1600;
            loadGraphic(AssetPaths.player__png,true,16,16);
            setFacingFlip(FlxObject.LEFT,false,false);
            setFacingFlip(FlxObject.RIGHT,true,false);
            animation.add("lr",[3,4,3,5],6,false);
            animation.add("u",[6,7,6,8],6,false);
            animation.add("d",[0,1,0,2],6,false);
            trace(animation.frameIndex);
            
        }
    
        override public function update(elapsed:Float):Void
        {
            movement();
            super.update(elapsed);
            trace(animation.getFrameIndex(frame));
        }
    
        function movement():Void
        {
            var _up:Bool = false;
            var _down:Bool = false;
            var _left:Bool = false;
            var _right:Bool = false;
    
            _up = FlxG.keys.anyPressed([UP,W]);
            _down = FlxG.keys.anyPressed([DOWN,S]);
            _left = FlxG.keys.anyPressed([LEFT,A]);
            _right = FlxG.keys.anyPressed([RIGHT,D]);
    
            //velocity.x = speed;
            //velocity.y = 0;
            
            if(_up || _down || _left || _right)
            {
                var mA:Float = 0;
                if(_up)
                {
                    mA = -90;
                    if(_left)
                        mA -= 45;
                    else if(_right)
                        mA += 45;
                    facing = FlxObject.UP;
                }
                else if(_down)
                {
                    mA = 90;
                    if(_left)
                        mA += 45;
                    else if(_right)
                        mA -= 45;
                    facing = FlxObject.DOWN;
                }
                else if(_left)
                {
                    mA = 180;
                    facing = FlxObject.LEFT;
                }
    
                else if(_right)
                {
                    mA = 0;
                    facing = FlxObject.RIGHT;
                }
                velocity.set(speed,0);
                velocity.rotate(FlxPoint.weak(0,0),mA);
    
                if((velocity.x != 0 || velocity.y != 0) && touching == FlxObject.NONE)
                {
                    switch(facing)
                    {
                        case FlxObject.LEFT, FlxObject.RIGHT:
                            animation.play("lr");
                        case FlxObject.UP:
                            animation.play("u");
                        case FlxObject.DOWN:
                            animation.play("d");
                    }
                }
            }
        }
    }
    

    thx :)



  • @nullstack I use trace(animation.getFrameIndex(frame)); in update() function to track the current frame and I found when I stop pressing keys, the animation stop at the last frame. e.g.when player facing left, the sprite will using the "lr" animation(because animation.add("lr",[3,4,3,5],6,false);), but when I stop pressing LEFT button, the number of the console output is 5,that is, the last frame of this animation(lr).



  • @nullstack oh! I check the source code on GitHub and found the solution, and....it works!!! the solution is:

    after the if(_up||_down||_left||_right) statement add this:

            else if(animation.curAnim!=null)
            {
                animation.curAnim.curFrame = 0;
                animation.curAnim.pause();
            }
    

Log in to reply
 

Looks like your connection to HaxeFlixel was lost, please wait while we try to reconnect.