1

I am trying to create a side scrolling game. In it, I want the player to be able to shoot blow darts.

As it is right now, if the player holds the Spacebar, the player's (which is a MovieClip) shooting animation loops. I want the animation to play only once per Spacebar press.

Here is code that I use:

This method determines which key(s) are being pressed:

        // Process the pressed key(s)
    public function KeyPressed(event:KeyboardEvent):void {
        // The Left Key was pressed
        if (event.keyCode == Keyboard.LEFT) {
            leftKeyPressed = true;
        }

        // The Right Key was pressed
        if (event.keyCode == Keyboard.RIGHT) {
            rightKeyPressed = true;
        }

        // The Up Key was pressed
        if (event.keyCode == Keyboard.UP) {
            upKeyPressed = true;
        }

        // The Down Key was pressed
        if (event.keyCode == Keyboard.DOWN) {
            downKeyPressed = true;
        }           
        // The Space Key was pressed
        if (event.keyCode == Keyboard.SPACE) {
            spaceKeyPressed = true;
        }
    } // End of 'KeyPressed()' function     

This method determines which key(s) have been released:

        public function KeyReleased(event:KeyboardEvent):void {
        // The Left Key was released
        if (event.keyCode == Keyboard.LEFT) {
            leftKeyPressed = false;
        }

        // The Right Key was released
        if (event.keyCode == Keyboard.RIGHT) {
            rightKeyPressed = false;
        }

        // The Up Key was released
        if (event.keyCode == Keyboard.UP) {
            upKeyPressed = false;
        }

        // The Down Key was released
        if (event.keyCode == Keyboard.DOWN) {
            downKeyPressed = false;
        }   

        // The Space Key was released
        if (event.keyCode == Keyboard.SPACE) {
            spaceKeyPressed = false;
        }
    } // End of 'KeyReleased()' function

This is a sample of code that plays one of the player's shooting animations:

if (onGround && !downKeyPressed && spaceKeyPressed) {
                    player.gotoAndStop(7);
}

How can I prevent the 7th player frame from continuously looping? I just want the animation to play once while the Spacebar is being pressed.

Thanks!

2 Answers2

2

Just add stop(); to the 7th frame of your movieclip.

But maybe you better use an enter frame event for such a game, here's an edited cut from a game I'm making:

    private var controls:Object = new Object();

    private function addListeners()
    {
        stage.addEventListener(KeyboardEvent.KEY_DOWN,controlKeyboard);
        stage.addEventListener(KeyboardEvent.KEY_UP,controlKeyboard);
        stage.addEventListener(Event.ENTER_FRAME,updateGame);
    }

    private function controlKeyboard(e:KeyboardEvent)
    {
        var keydown:Boolean = new Boolean();

        if (e.type == "keyDown")
        {
            keydown = true;
        }

        controls[e.keyCode] = keydown;
    }

    private function updateGame(e:Event)
    {
        if (controls[Keyboard.SPACE])
        {
            if (player.currentFrame == 1)
            {
                player.gotoAndPlay(2);
            }
        }

        if (player.currentFrame == 7)
        {
            player.gotoAndStop(1);
        }

    }
  • Thanks, Thomas. However, I just tried this: ` if (player.currentFrame == 6) { player.gotoAndStop(2); } ` (Frame 6 is the player's shoot animation - Frame 2 is the player's standing still frame.) This immediately stopped the shoot animation and went to the standing frame. This happened so fast that you could not always see the shooting animation. – Christian Basar Nov 06 '11 at 05:12
  • The shoot animation is on frame 6, so you mean there's a movieclip with the animation on frame 6 or you mean that the animation begins on frame 2 and ends on frame 6? – Thomas Blommaert Nov 06 '11 at 09:50
  • I tried to follow the code you posted. Frame 6 is where the player's shooting animation is. Frame 2 is the standing animation. In doing this, I tried to have the player go through the shooting animation once and then go right back to standing. – Christian Basar Nov 07 '11 at 00:23
  • I still don't get it, so: Option 1: You wan't to go from frame 6 to frame 2, like "rewinding". Option 2: The whole shoot animation is on frame 6, in a movieclip, and after it has played, you want to skip to frame 2? – Thomas Blommaert Nov 09 '11 at 17:16
0

Just a couple of quick ideas.

1) Add a flag to indicate the player is currently animating, isThrowing = false; for instance. Set it to true when you set off the first dart and add it to the if statement above. At the end of the animation loop, set it to false again.

2) Another way would be to remove the event listeners (or some of them) when you send the dart off and only add them again when the dart is done.

There are probably other ways, but these should give you some venues of exploration.

Good luck!

izk
  • 1,189
  • 2
  • 8
  • 23