please quote next time- so we know what context you are speaking of
here it is used in actionscript:
PHP Code:
var pause:Boolean = true;//default state is pause equals true
_root.onEnterFrame = function(){//onEnterFrame loop,-
if (!pause){//put after this curly brace your game loop code
trace("engine runs....");
}
}
button_pauseSwitch.onPress = function(){//here is a button that unpauses or pauses the game
pause=!pause;//switch pause state
}
Haha! I know, it looks weird, but it's actually quite beautiful! It prevents your script from running if the boolean pause is true, since it will only run if pause is false. "do everything" is the content of your main functions, such as this.onEnterFrame and so on.
Last edited by Clownfish5; 09-09-2008 at 05:37 PM.
Just for the input, here's how to stop all animations in your movie using a simple recursive function.
Code:
function StopAnimate(target) {
for (var a in target) {
if (typeof (target[a]) == "movieclip") {
target[a].stop();
StopAnimate(target[a]);
}
}
}
StopAnimate(this);
This code made me think, by time based comparison of frames we can store which mc's are static and which are dynamic, and we can play the dynamic one's.
This code pauses and unpauses:-
Code:
function StopAnimateStep1(target) {
for (var a in target) {
if (typeof (target[a]) == "movieclip") {
//store mc reference and frame number
mcArray.push([target[a],target[a]._currentframe,"STATIC"]);
StopAnimateStep1(target[a]);
}
}
}
function StopAnimateStep2() {
clearInterval(intervalId);
for(a in mcArray)
{
mcArray[a][0].stop();
//check if the frame has changed, that means its dynamic
if(mcArray[a][1] != mcArray[a][0]._currentframe)
{
mcArray[a][2] = "DYNAMIC";
}
}
gamePauseState = "PAUSED";
}
function StopAnimate(target)
{
//set gamePauseState
gamePauseState = "PROCESSING";
StopAnimateStep1Done = false;
//format of this array
//mcArray = [[mcRef,mcRefInitialFrame,animState]
mcArray = [];
StopAnimateStep1(this);
intervalId = setInterval(StopAnimateStep2,100);
}
function StartAnimate() {
for (var a in mcArray) {
if (mcArray[a][2] == "DYNAMIC") {
mcArray[a][0].play();
}
}
gamePauseState = "NOT_PAUSED";
}
//string var to store the state of pausing
gamePauseState = "NOT_PAUSED";
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
if (Key.isDown(Key.SPACE)) {
if(gamePauseState == "NOT_PAUSED")
{
StopAnimate(this);
}
else if(gamePauseState == "PAUSED")
{
StartAnimate();
}
}
};
Key.addListener(keyListener);
Check the attached fla for the code in action.
Last edited by adit_ya_sharma; 09-11-2008 at 11:45 PM.
One logical problem that remains with this method is that when the script commands a mc to stop on a particular frame from another frame and simultaneously the pause function is called, it will store the mc as 'dynamic', while infact it is 'static' and it was just stopped on a frame.
For example:-
Code:
myMc.gotoAndStop(1);
someTimer = 100
onEnterFrame = function()
{
someTimer--;
if(someTime == 0)
{
myMc.gotoAndStop(2);
//simultaneously before 'myMc' was stopped on second
//frame, if pause key is pressed, the function will think this
//mc is 'dynamic' by time comparision, infact it is 'static'
}
}
To avoid this situation, use a var as a flag to stop the further execution of the statements in your main game loop, make sure you queue the calling of the pause function at the end of all the statements of the main loop, so that all the action is executed and locked by the flag, and then finally the time check can be done for which mc's are 'dynamic' and which are 'static'.
If you still don't get it, I will post an example.
Last edited by adit_ya_sharma; 09-11-2008 at 02:38 AM.
Ok, so I came to the conclusion that time based method will not be accurate using setInterval, due to which I came up with a method in which you store all mc's frames, let the swf play 1 extra frame, compare with the previous frames of all mc's, and set them back to there previous frame and storing its behaviour as "DYNAMIC", else they will be "STATIC".
The logic for the game loop code though has to be the same as I had explained earlier for avoiding that situaton.
It would really help if someone can test this out. I have attached the swf and fla file with comments so that its easier to implement.
Last edited by adit_ya_sharma; 09-11-2008 at 11:45 PM.
That seems like a really CPU heavy function to stop a couple of movie clips, adit.
The 'Boose':
ASUS Sabertooth P67 TUF
Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
8GB G.Skill Ripjaws 1600 DDR3
ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
New addition: OCZ Vertex 240GB SATA III SSD WEI Score: 7.6
Agreed, if lot of mc's will be present probably it will hang the game, but for most games with less number of mc's (one I just tested out) it worked well.
Edit: Small error in previous attachment, now fixed.
Edit2: Also thought I will mention that not necessarily you have to pause the game using this, thanks to SaphuA's format, you can target a particular mc to be brought to a stand still, including its child mc's, so now you can easily stop a particular mc.
For example, Sub-Zero hits Liu Kang with the ice, so it will be easy to show him frozen while the collision can still be checked for an uppercut
Last edited by adit_ya_sharma; 09-12-2008 at 12:08 AM.
Kind of a waste to bump a year old topic to attack a spammer and post an fla that doesn't really get the job done...
The 'Boose':
ASUS Sabertooth P67 TUF
Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
8GB G.Skill Ripjaws 1600 DDR3
ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
New addition: OCZ Vertex 240GB SATA III SSD WEI Score: 7.6