Animation MousePressed complete the animation


(Sharon Shalom Iluz) #1

Hey guys I am new to FlashPunk and to ActionScript 3 my situation is have 2 animation with in the same sprite sheet anim1 = from 0 to 74 anim2 = from 75 to 95

my question is: Is there an event listener for the animation to compete then on the next mouse press to start over always showing the animation from start to end.

code:

[Embed(source = ‘Assets/sheet_8x12.png’)] private const DUCK_2:Class;

    public var _duck:Spritemap = new Spritemap(DUCK_2, 125, 225)

    public function DuckEntity ()
    {
        _duck.add("idle", [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73], 30, true);
        _duck.add("hit",[74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94], 30, false);
        graphic = _duck;
        _duck.play("idle");
    }
    override public function update():void
    {
        if(Input.mousePressed){
            _duck.play("hit");
        }
        if(Input.mouseReleased){
            _duck.play("idle")
        }

    }

(Justin Wolf) #2

I think what you’re looking for is the final parameter in your Spritemap's initialization, which is a callback function that gets called whenever an animation finishes playing. I’ll show you an example:

public var _duck:Spritemap = new Spritemap(DUCK_2, 125, 225, onComplete);

public function onComplete():void
{
     // check which current animation we're on
     // i prefer 'switch' instead of if/else ifs. use whatever you're comfortable with.
     switch (_duck.currentAnim)
     {
          // if 'hit' animation is the currentAnim,
          // play 'idle' animation when 'hit' animation completes.
          case "hit":
               _duck.play("idle");
               break;
     }
}

You can also simplify your animation’s frames by doing the following:

_duck.add("idle", FP.frames(0, 73), 30, true);
_duck.add("hit", FP.frames(74, 94), 30, false);

I may also be completely misinterpreting your post, however. So, disregard if this is of no use to you. It was a bit hard to understand. In your example, your ‘hit’ animation will never actually have time to play because immediately after you release your mouse, the ‘idle’ animation is triggered to play. Using my code above, you can completely remove the mouseReleased line you’ve got. Clicking will then simply play the ‘hit’ animation and then when it finishes, it’ll advance back to the ‘idle’ animation.


(Sharon Shalom Iluz) #3

hey thanks for the reply but i don’t think its it

i have a duck animation that is idleling and i want to make the duck a button so when i press on it the animation hit starts and will finish till the end once its completed researched the last frame go back to idle animation.


(Sharon Shalom Iluz) #4

Found the answer

public function added(): void { var mySpritemap: Spritemap = new Spritemap(blah); mySpritemap.callback = functionToCall; }

private function functionToCall(): void { //gets called at the end of the animation }


(Justin Wolf) #5

That’s exactly what I wrote except the callback was declared in the initialization of the Spritemap (in the fourth parameter) instead of after the Spritemap was made. Unless of course I’m still misunderstanding! Regardless, glad you’ve solved the problem!