Sign in to follow this  

Flash (AS 2.0) game problem

This topic is 2659 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, I'm trying to program a side scroller where the player can move left and right and attack. If the player is not performing an action (in this case walking or attacking) it plays an idle animation called 'still'.

My problem is that if the player is holding left or right and presses the spacebar (attack), and then releases the left or right key, the player will stop (as they should) but the walking animation will continue to play as though they were still moving.

In other words: if the player is holding the left or right key and presses attack, performingAction will become true and remain true even when key is released.

This problem is fixed the moment the player changes directions or attacks while standing still.


If anyone could help me figure this out I'd really appreciate it.

onClipEvent (load) {

speed = 15; //player speed
scale = _xscale //player x scale
performingAction = false; //is the player performing an action
attacking = false; //is the player attacking
keyheld = false; //is the attack key being held down

}
onClipEvent (enterFrame) {


if(!performingAction){ //if the player is not performing action, play the 'still' animation (idle)
gotoAndPlay('still');
}


if (Key.isDown(Key.LEFT) && !attacking) { //if the left arrow key is down and the player is not attacking, play the walking animation, flip the x scale, and change performing action to true
gotoAndStop('walking');
_x -= speed;
_xscale = -scale;
performingAction = true;

}

var leftKeyUpListener = new Object(); //left key up listener: if the left key is up, change performing action back to false
leftKeyUpListener.onKeyUp = function() {
if (Key.LEFT == Key.getCode()) {
performingAction = false;

}
}



if (Key.isDown(Key.RIGHT)&& !attacking) { //if the right arrow key is down and the player is not attacking, play the walking animation, flip the x scale, and change performing action to true
gotoAndStop('walking');
_x += speed;
_xscale = scale;
performingAction = true;
}

var rightKeyUpListener = new Object(); //right key up key listener: if the right key is up, change performing action back to false
rightKeyUpListener.onKeyUp = function() {
if (Key.RIGHT == Key.getCode()) {
performingAction = false;

}
}

var spaceDownKeyListener = new Object(); // space down key listener: if the space key is down, play the attack animtion, attacking is true, attack key held is true, and performing action is true;
spaceDownKeyListener.onKeyDown = function() {
if (Key.SPACE == Key.getCode() && !keyheld && !attacking) {
gotoAndPlay('attack');
attacking = true;
keyheld = true;
performingAction = true;

}
}



var spaceUpKeyListener = new Object(); //space key up listener: if the space key is up the attack key held is false. performing action and attacking both become false when the attack animation finishes (not shown here)
spaceUpKeyListener.onKeyUp = function() {
if (Key.SPACE == Key.getCode()) {
keyheld = false;

}
}



Key.addListener(leftkeyUpListener);
Key.addListener(rightKeyUpListener);
Key.addListener(spaceUpKeyListener);
Key.addListener(spaceDownKeyListener);
Key.addListener(WeaponKeyListener);
}




thanks for reading

Share this post


Link to post
Share on other sites

This topic is 2659 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this