PDA

View Full Version : [AS2] Pausing then unpausing a game


M1KE
02-19-2009, 10:57 PM
Hi! I have a game hear that I need to unpause and pause it with the letter p (key 80). The pausing works and the unpausing works but you have to be quick. For instance if you hold down p for a long period of time it flash between the paused state and the unpaused state. Is there a way how I can set a time here is what i have:

function Pause() {
if (Key.isDown(80)) {
if (Paused) {
setInterval(Pause,3000)
Paused = false;
} else {
Paused = true;
setInterval(Pause,3000)
}
}
if (Paused) {
_root.Pac.Move = false;
_root.Pac.gotoAndStop(1);
_root.Monster1.Move = false;
_root.Monster2.Move = false;
_root.Monster3.Move = false;
_root.Monster4.Move = false;
_root.Monster5.Move = false;
_root.Pac._alpha = 15;
_root.PauseText._alpha = 100;
_root.Monster1._alpha = 15;
_root.Monster2._alpha = 15;
_root.Monster3._alpha = 15;
_root.Monster4._alpha = 15;
_root.Monster5._alpha = 15;
_root.Food._alpha = 15;
} else if (Paused == false) {
_root.Pac.play();
_root.Pac.Move = true;
_root.Monster1.Move = true;
_root.Monster2.Move = true;
_root.Monster3.Move = true;
_root.Monster4.Move = true;
_root.Monster5.Move = true;
_root.Pac._alpha = 100;
_root.PauseText._alpha = 0;
_root.Monster1._alpha = 100;
_root.Monster2._alpha = 100;
_root.Monster3._alpha = 100;
_root.Monster4._alpha = 100;
_root.Monster5._alpha = 100;
_root.Food._alpha = 100;
}
}
I'm trying to make it pausing when you press 'p' but you can't unpause it till 3 seconds later to stop the flash thing. the function 'Pause is then put in the actions for Pac. 9If you've guesseed this is a Pac man sort of game.
Thanks in advance!

rrh
02-20-2009, 03:29 PM
You put this in an onEnterFrame method? I think you can put it in an onKeyDown.

PhatKitty
02-20-2009, 04:16 PM
Maybe use a Boolean variable instead of a function? I'm kinda new to AS so it might be wrong

var Paused:Boolean = false

if (Key.isDown(80)) {
Paused = true;
}
if (Paused == true) {
if (Key.isDown(80)) {
Paused = false;
}
_root.Pac.Move = false;
_root.Pac.gotoAndStop(1);
_root.Monster1.Move = false;
_root.Monster2.Move = false;
_root.Monster3.Move = false;
_root.Monster4.Move = false;
_root.Monster5.Move = false;
_root.Pac._alpha = 15;
_root.PauseText._alpha = 100;
_root.Monster1._alpha = 15;
_root.Monster2._alpha = 15;
_root.Monster3._alpha = 15;
_root.Monster4._alpha = 15;
_root.Monster5._alpha = 15;
_root.Food._alpha = 15;
} else if (Paused == false) {
_root.Pac.play();
_root.Pac.Move = true;
_root.Monster1.Move = true;
_root.Monster2.Move = true;
_root.Monster3.Move = true;
_root.Monster4.Move = true;
_root.Monster5.Move = true;
_root.Pac._alpha = 100;
_root.PauseText._alpha = 0;
_root.Monster1._alpha = 100;
_root.Monster2._alpha = 100;
_root.Monster3._alpha = 100;
_root.Monster4._alpha = 100;
_root.Monster5._alpha = 100;
_root.Food._alpha = 100;
}

This would mean (I think) that when you press p it pauses, and when you press it again it unpauses, so you don't need an interval, unless you wanted an interval?

And sorry if this is completely wrong ;)

M1KE
02-20-2009, 04:44 PM
The Boolean thing half worked because it paused it and didn't flash between the two states but the only problem was that you had to hold down the p key to keep it paused

M1KE
02-20-2009, 04:56 PM
How do you do that. How does the computer know which key. I looked into it and it got me puzzled

PhatKitty
02-20-2009, 05:02 PM
How do I do what? And lemme try and fix the problem.

PhatKitty
02-20-2009, 05:05 PM
Try:

var Paused:Boolean = false

if (Key.isDown(80)) {
Paused = true;
}
if (Paused == true && (Key.isDown(80))) {
Paused = false;
}
if (Key.isDown(80)) {
then the rest of the monster = false stuff

M1KE
02-20-2009, 05:34 PM
KeyListener = new Object();
KeyListener.onKeyDown = function() {
var KeyCode = Key.getCode();
if (KeyCode == 80) {
if (Paused) {
var Paused:Boolean = false;
} else {
var Paused:Boolean = true;
}
}
Thats what I tried but it doesn't even recognise that 'p' is being pressed I checked it with a trace command

PhatKitty
02-20-2009, 05:40 PM
I think you have to state the variables at the top of the script, then after that you only need to put Paused = false/true:

var Paused:Boolean = false;

KeyListener = new Object();
KeyListener.onKeyDown = function() {
var KeyCode = Key.getCode();
if (KeyCode == 80) {
if (Paused) {
Paused = false;
} else {
Paused = true;
}
}

rrh
02-20-2009, 05:45 PM
Maybe I should have said onKeyUp ? It's been a long time since I used as2 so I sometimes forget these things.