dcsimg
A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 28

Thread: Stopping a MC's internal animation with a global Pause

  1. #1
    Senior Member
    Join Date
    Aug 2005
    Posts
    238

    Stopping a MC's internal animation with a global Pause

    Hello all,

    I have recently added a pause function to a game I am working on and it almost completely works. My only problem is that movie clips continue their own animations. Does anyone know the simplest way I can get this to stop?

    The code I have so far, which doesn't work properly, is below. paused is self explanatory but its also worht noting that this code is associated with the enemy movie clip that is in the library, which is then being exported to attached instances of it. The problem I have noticed is that this._currentframe is always tracing as "1" even when the clip is clearly in the middle of its animation. Any help would be much appreciated. Thank you!

    code:


    if (_root.paused == false) {
    this.gotoAndPlay(this._currentframe);
    } else {
    this.gotoAndStop(this._currentframe);
    }



  2. #2
    Member
    Join Date
    Aug 2005
    Location
    Vienna, Austria
    Posts
    56
    how about

    Code:
    if (_root.paused == false) {
    	play();
    } else {
    	stop();
    }

  3. #3
    Senior Member
    Join Date
    Aug 2005
    Posts
    238
    This doesn't work; it does the same thing as my previous attempt. That being when i hit the pause button, the clip finishes its current animation, then stops, and when unpaused it does not resume, it stays at frame 1 of its animation. Any ideas? Thank you in advance.

  4. #4
    Member
    Join Date
    Aug 2005
    Location
    Vienna, Austria
    Posts
    56
    can you show us the fla?
    that would help

  5. #5
    383,890,620 polygons nGFX's Avatar
    Join Date
    Oct 2002
    Location
    Germany / Ruhrgebiet
    Posts
    902
    well, it would work if you pasted the code to *every* frame ...

    script is always executed on the frame of it is written.

    you could place it in an onEnterFrame event, though.
    (and play() and stop() is enough, too)

    nGFX

  6. #6
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    Or you could just make it so that instead of setting a 'paused' variable to 'true', and making all the movie clips wait to react to it at all times, just use this:

    for (var i in _root) {
    _root[i].stop();
    }

    This script loops through all the children of the main timeline and tells them to stop playing.

    Making them play again would be done like this:

    for (var i in _root) {
    if (!_root[i].stopped) {
    _root[i].play();
    }
    }

    The reason I added the "if it's not stopped" condition is that some things might not have meant to be played. For instance, if you told something to stop playing BEFORE pausing, unpausing would still make it play again. To fix this problem, do this:

    Whenever you have a stop(); script on one of the frames of your movie clips, add this line after it:

    stopped = true;

    This means that if you pause and unpause, something that was originally under a stop(); action will remain so.

    I used this method of pausing in Commando 2. Of course, since a lot of the movie clips also had nested movie clips in them, I had to tweak it a little. I also had to stop the movement scripts running. But it still worked perfectly.
    http://www.birchlabs.co.uk/
    You know you want to.

  7. #7
    doItLikeThis
    Join Date
    Jan 2004
    Location
    :noitacoL
    Posts
    1,080
    It depends on the way you make your game. For example, I have a player MC with all other MCs in it as different frames which have different animations. I name there instances as 'current_mc' and in my main player mc the code is:-

    code:

    onEnterFrame = function()
    if(_global.paused == false)
    {
    current_mc.play();
    }
    else
    {
    current_mc.stop();
    }
    }



    Hope that helps a bit.

    edit: spell check
    -Aditya

  8. #8
    Senior Member
    Join Date
    Apr 2005
    Posts
    104
    Vengeance, that code is awesome. I had no clue you could do that. Even though I didn't ask the question, I want to thank you for the info. I'm using an adaptation of that code, and it works perfect.

  9. #9
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    =D I knew it would help someone.
    http://www.birchlabs.co.uk/
    You know you want to.

  10. #10
    Senior Member
    Join Date
    Aug 2005
    Posts
    238
    wow, thanks for the huge amount of response guys, I'll let you know if i get it working

  11. #11
    Senior Member
    Join Date
    Aug 2005
    Posts
    238
    Vengeance the code works wonderfully except for my nested movie clips, which still animate... how did you modify the code to work in this case, if you don't mind my asking. Thank you

  12. #12
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    Well, the easiest way is just to write down all the instance names of all the nested mcs you have, and modify the script accordingly, like so:

    for (var i in _root) {
    mc = _root[i];
    mc.stop();
    mc.thrusters.stop();
    mc.turret.stop();
    mc.muzzleFlash.stop();
    }

    So if the loop finds anything nested in any of those movie clips with an instance name of 'thrusters', 'turret', or 'muzzleFlash', it'll tell those to stop as well.

    for (var i in _root) {
    mc = _root[i];
    if (!mc.stopped) {
    mc.play();
    }
    if (!mc.thrusters.stopped) {
    mc.thrusters.play();
    }
    if (!mc.turret.stopped) {
    mc.turret.play();
    }
    if (!mc.muzzleFlash.stopped) {
    mc.muzzleFlash.play();
    }
    }

    That's the script you'd use to unpause them, too. Remember, if you have any frames inside the nested mcs with a stop(); script on them, you need to set their 'stopped' variable to true, as with last time.
    http://www.birchlabs.co.uk/
    You know you want to.

  13. #13
    Untitled-1.fla strille's Avatar
    Join Date
    Mar 2001
    Location
    Sweden
    Posts
    1,626
    If you want to pause all movie clips, including nested ones, you can use a recursive function that iterates through all movie clips and pauses them (when a movie clip is found it is stopped, and then all its children are stopped and so on...).

    Code:
    function stopAllMovies(instance) {
    	instance.stop();
    	for (var n in instance) {
    		if (typeof(instance[n]) == "movieclip") {
    			arguments.callee(instance[n]);
    		}
    	}
    }
    
    stopAllMovies(_root);

  14. #14
    Senior Member
    Join Date
    Aug 2005
    Posts
    238
    Wow guys, great idea using the recursion, hadn't even occured to me! All this pausing discussion has made me think of a couple of more questions:

    1) how do you pause a sound clip (like background music) so that when you unpause it resumes exactly where it left off and not restart from the beginning.

    2) if you wanted to have a game with a menu (say regular game code on main timeline frame 1 and menu on frame 2 etc) what is the best way to implement the code for that. I imagine it is related to this pause discussion because in a sense when you hit the menu button, you want all the game action to "pause" and control to pass over to the menu scene, and vice versa. Any help you could offer would be much appreciated. Thank you so much guys!!

  15. #15
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    Use this to start playing the music in the first place:

    _root.mySound = new Sound();
    _root.mySound.attachSound("music");
    _root.mySound.start(null, 10000);
    // If it's not playing, try 0 instead of null in the previous line of script...

    Use this to pause it:

    _root.pausePos = _root.mySound.position;
    stopAllSounds();
    // If you just want to stop the music, use _root.mySound.stop(); instead.
    // If you only want it to be quiter, use _root.mySound.setVolume(50);
    // Change the parameter '50' to be a number between 0 and 100, depending on how loud you want it to be.

    Use this to unpause it:

    _root.pausePos /= 1000;
    _root.mySound.start(_root.pausePos, 1000);
    // If this doesn't work, put this script below line 1: _root.pausePos = int(_root.pausePos);

    Well, if any of that didn't work, it's because all of that was a complete guess.
    http://www.birchlabs.co.uk/
    You know you want to.

  16. #16
    Senior Member
    Join Date
    Aug 2005
    Posts
    238
    Works like a charm Vengeance! Now, if you can answer my menu question, then you will officially be the man. Officially

  17. #17
    Senior Member
    Join Date
    Aug 2005
    Posts
    238
    Oh, I detected a slight problem with the music restart code, Vengeance. Its the line " _root.mySound.start(_root.pausePos, 1000);" what this does is restart the music at the right place, but each time it loops after that its starting forrm that offset, which isn't correct. I am currently trying methods of getting around this using getTimer()... but i haven't quite got it yet. If you have any ideas please let me know. Thanks!



    P.S.

    I have tried some things to the effect of:

    code:

    onClipEvent (enterFrame) {
    if (getTimer() >= _root.musicResetTimer + (_root.standardBG.duration - _root.musicPausePosition) && _root.restartMusic == true) {
    _root.standardBG.start(0, 10000);
    _root.restartMusic = false;
    }

    }



    where musicResetTimer is the time the pause button was pressed. This handles the case after the first restart occurs in the manner specified above. This doesn't seem to work properly though, it restarts, but always too late. I'm sure I am missing something...but i'm too tired to see it....any thoughts?
    Last edited by Scotopia; 08-23-2005 at 10:58 PM.

  18. #18
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    How about using this to unpause it:

    _root.mySound.start(0, 1000);
    _root.mySound.position = _root.pausePos;

    This should work, unless the Sound object's position property is read-only. If it doesn't work, try this:

    _root.pausePos /= 1000;
    _root.mySound.start(_root.pausePos, 0);
    _root.mySound.onSoundComplete = function() {
    _root.mySound.start(0, 10000);
    _root.mySound.onSoundComplete = null;
    };

    :P
    http://www.birchlabs.co.uk/
    You know you want to.

  19. #19
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    Quote Originally Posted by strille
    If you want to pause all movie clips, including nested ones, you can use a recursive function that iterates through all movie clips and pauses them (when a movie clip is found it is stopped, and then all its children are stopped and so on...).

    Code:
    function stopAllMovies(instance) {
    	instance.stop();
    	for (var n in instance) {
    		if (typeof(instance[n]) == "movieclip") {
    			arguments.callee(instance[n]);
    		}
    	}
    }
    
    stopAllMovies(_root);
    hey strille how come you use "arguments.callee(instance[n])" and not "stopAllMovies(instance[n]);". just wonderin
    Graphics Attract, Motion Engages, Gameplay Addicts
    XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro

  20. #20
    Untitled-1.fla strille's Avatar
    Join Date
    Mar 2001
    Location
    Sweden
    Posts
    1,626
    No reason really... Other than it works for anonymous recursive functions as well. It also underlines that the function is recursive, so I think it's more readable and clear exactly what it is doing.

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