Movie plays until a certain frame?

7 posts

Flag Post

Hi, if someone could help me with an easy problem I’d be greatful.

Right now I have a movie in one of my scenes. Based on a variable, I tell that movie to go to a certain frame. So in my scene “Actions” code I have

mymovie.gotoAndStop (myvariable);

What I really want it to do is to play that movie up until myvariable. Is there something like:

mymovie.playAndStopAtFrame (myvariable);

Or is there another way to do this? Have my movie stop at a certain frame based on a variable? I’m sure this is simple but I can’t figure it out. Thanks!!!

-Sage880

 
Flag Post

There’s no playAndStop or anything like that. I guess the best way is just to set up an onEnterFrame function

  • var startFrame = 10;
  • var endFrame = 40;
  • myMovie.gotoAndPlay(startFrame);
  • myMovie.onEnterFrame = function() {
  • if (myMovie._currentframe == endFrame) {
  • myMovie.stop();
  • delete myMovie.onEnterFrame;
  • }
  • }
 
Flag Post

You’re a legend, that works perfectly. Thanks!

Can I ask what the “delete myMovie.onEnterFrame” line is for? Why is that line needed? I’m sorry – I haven’t programmed anything in about 7 years and forget most of the basics.

 
Flag Post

It just removes that function since it isn’t needed any more. Once it’s reached the end frame it’ll be running pointlessly every frame.

Realistically the amount of strain that’s going to cause is almost nothing.. but nice to be tidy, yknow :)

 
Flag Post

Cool. Yup, better to be a good programmer then a bad one! Guess I was spoiled by Java and it’s automatic garbage collection so I totally forget about things like memory leaks.

Appreciate all your help. Thanks again!

 
Flag Post

Well technically it wouldn’t be a leak. Flash also has automatic garbage collection :)

Once the MovieClip itself was cleared for garbage collection the function would be too. It’s just the time while the MovieClip is still there that it’ll be saving a tiny bit of processing by removing the onEnterFrame function (since its automatically called every frame while its on there).

The delete keyword is kinda misleading if you’ve used other languages. It doesn’t actually delete it then and there, it just frees it up for garbage collection the same as if you’d done myMovie.onEnterFrame = null;

 
Flag Post

Ah yes, I see now. Very interesting. You can do a lot more with Flash and AS than it seemed to me when I started learning about it. I’m using AS2 right now and it’s a very programmer-friendly language. I’ve heard AS3 was even better – I might try and tackle that for my next game.