A Flash Developer Resource Site

Page 2 of 2 FirstFirst 12
Results 21 to 36 of 36

Thread: How to pause a game?

  1. #21
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    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


  2. #22
    Humble Flash Newbie
    Join Date
    Feb 2007
    Posts
    107
    Quote Originally Posted by flashkid.com
    If(!pause)
    {
    do everything
    }

    Can you elaborate on the above?
    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.

  3. #23
    talk to the hand! Never_land:('s Avatar
    Join Date
    Nov 2007
    Location
    your mother's bedroom
    Posts
    490
    thanks this helps a lot.
    pause=!pause

  4. #24
    doItLikeThis
    Join Date
    Jan 2004
    Location
    :noitacoL
    Posts
    1,080
    Quote Originally Posted by SaphuA
    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.
    -Aditya

  5. #25
    doItLikeThis
    Join Date
    Jan 2004
    Location
    :noitacoL
    Posts
    1,080
    For Pausing and Unpausing the mc animation including the root, check the new attached file.
    Last edited by adit_ya_sharma; 09-11-2008 at 11:45 PM.
    -Aditya

  6. #26
    doItLikeThis
    Join Date
    Jan 2004
    Location
    :noitacoL
    Posts
    1,080
    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.
    -Aditya

  7. #27
    doItLikeThis
    Join Date
    Jan 2004
    Location
    :noitacoL
    Posts
    1,080
    Ok I will post afterwards in detail, this is getting confusing and buggy.
    -Aditya

  8. #28
    doItLikeThis
    Join Date
    Jan 2004
    Location
    :noitacoL
    Posts
    1,080
    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.
    -Aditya

  9. #29
    Game Player - Developer scheletro's Avatar
    Join Date
    Mar 2005
    Location
    México living in Barcelona
    Posts
    1,121
    Is this thread still alive?
    WOOOW


    "I love to make them as I love to play them"

  10. #30
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    I doubt it, somehow its very uncoordinated: everyone posts their own kind of reply to the extremely vague and almost pointless question.

  11. #31
    doItLikeThis
    Join Date
    Jan 2004
    Location
    :noitacoL
    Posts
    1,080
    Sad that no one actually saw my last post, could have actually helped alot, anyway let this die off as usual.
    -Aditya

  12. #32
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    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

  13. #33
    doItLikeThis
    Join Date
    Jan 2004
    Location
    :noitacoL
    Posts
    1,080
    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
    Attached Files Attached Files
    Last edited by adit_ya_sharma; 09-12-2008 at 12:08 AM.
    -Aditya

  14. #34
    the cheesy child bounceboy's Avatar
    Join Date
    Dec 2008
    Location
    Australia
    Posts
    323
    i made up a little FLA file then but how do i upload it?

    well... here?

    PHP Code:
    Uh... wait a tick... thats wrong isn't it? 
    pauser.fla

    sasuke82 made 2 posts. wow. what a record???

    i bet the other 81 numbers was him switching accounts because he kept getting kicked out!
    Last edited by bounceboy; 03-27-2009 at 08:40 AM.

  15. #35
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    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

  16. #36
    the cheesy child bounceboy's Avatar
    Join Date
    Dec 2008
    Location
    Australia
    Posts
    323
    true.... true... but the fla is an example like somebody wanted...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center