-
Key.isDown HELP
I have the following code as a pause button as my game is run threw a timer...
Code:
onClipEvent (enterFrame) {
_root.frame = _root.timer._currentframe;
if ((Key.isDown(Key.SPACE)) and (_root.pauser._currentframe == 1)) {
_root.timer.gotoAndStop(_root.frame);
_root.pauser.gotoAndStop(2);
} else if ((Key.isDown(Key.SPACE)) and (_root.pauser._currentframe == 2)) {
_root.timer.gotoAndPlay(_root.frame);
_root.pauser.gotoAndStop(1);
}
}
When i press space the pause MC appears and then disperes. I know why this happens but i cant figure out a way around it...
-
it happens because Key.isDown returns true if the key is down, not when it's pressed. the game is probably going through more than one frame in which the spacebar is down, because you can't press it and let up fast enough. here's my solution:
Code:
spaceDown = false;
onClipEvent (enterFrame) {
_root.frame = _root.timer._currentframe;
if (Key.isDown(Key.SPACE)){
if (_root.pauser._currentframe == 1 && !spaceDown){
_root.timer.gotoAndStop(_root.frame);
_root.pauser.gotoAndStop(2);
} else if (_root.pauser._currentframe == 2 && !spaceDown){
_root.timer.gotoAndPlay(_root.frame);
_root.pauser.gotoAndStop(1);
}
spaceDown = true;
} else {
spaceDown = false;
}
}
this is if you want to keep everything in an onEnterFrame function. tell me if that helps.
EDIT: sorry, i made a mistake, the spaceDown variable needs to be defined outside the onEnterFrame function or else it'll be reset every frame. It's fixed now.