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!