1
votes

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

2 Answers

2
votes

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);
        }

    }
0
votes

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!