views:

890

answers:

6

I want my movieclip to play once and stop on the last frame. I use the following code in the loop of my movieclip class. (this is as3)

if(currentFrame == 120)
stop();

120 is the last frame. It plays once. but the problem is it goes back to frame 1 again. is there a better method of stopping a movieclip on a particular frame.

+2  A: 

Hi numerical25,

On the timeline, you can just put a keyframe at the end, and add the stop(); method there. When it reaches that frame, the stop(); method will execute, and the clip will stop.

Assuming you are using the timeline that is, but sounds like you are.

Tyler Egeto
i am using a custom class attached to the movieclip. I would like to do it from there but if I cant, I guess I would have to settle with your solution.
numerical25
I gave that a try and it didnt work. it still started from frame 1 again. i traced the currentFrame. shows it goes back to 1
numerical25
Well, this means there is something else going on. Are you working with nested MovieClips to create the animation? Are they being stopped correctly? Perhaps if you can share more about how you have things setup, we can help you track it down.
Tyler Egeto
A: 

This seems more along the lines of what you're looking for:

addEventListener(Event.ENTER_FRAME, function(e:Event){
    if(currentFrame == totalFrames){
      stop();
      removeEventListener(event.type, arguments.callee);
    }
});

I added an event listener (for Actionscript 3) so on every frame this function will fire and check what frame it is on. If it's the last frame it will stop. Based on your example I'm presuming you have a class that's extending MovieClip.

Newtang
This would work, but is a pretty poor solution in terms of performance. This enterframe is going to keep firing afterwards, with no way of cleaning it up. A better dynamic solution similar to this, would be to use the addFrameScript method. Of course if you had a reference to the function, you could clean it up, and it would be fine.
Tyler Egeto
I agree with Tyler. Anonymous functions are also bad, since they sit in memory for much longer before they are gc'ed.
alecmce
So I guess the stop() function in the timeLine is the way to go ??. I am actually using a single loop for all my movieclips, So I would prefer if it was not in an event listener. thanks for the responses guys
numerical25
I tried the first guys solution. but it goes back to 1 again. I traced it.
numerical25
Now I am just figuring it it doesnt want to stop even at the first frame. I dont know whats going on. i put stop() in the movieclip constructor. I only have one timeline within the movieclip that exceeds 1 frames. any other movieclips only have 1.
numerical25
could it be possibly the type of tweening I am using ??
numerical25
I edited my example to account for the performance issue you guys mentioned. I neglected to include it to err on the side of simplicity but that was a mistake in retrospect.It seems like the stop() solution should be the best one.
Newtang
A: 

from messing around wit it, I know the problem has nothing to do with flash not responding to my stop() function. I believe it may have something to do with flash calling the movieclip twice. One on top of another. Making it look like it started over again. I am not sure. If I call the movieclip anywhere else, it works. just not in my loop. I did do a trace and the trace only showed up once which shows it initiated it once.

numerical25
+3  A: 

If you want to do it with actionscript and not add a stop() to the last frame on the timeline manually, then you can use the undocumented addFrameScript() method.

mc.addFrameScript(mc.totalFrames - 1, function():void 
{
    mc.stop();
});

Can't remember the scope of the function, but you can either use mc.stop(), or just stop().

*EDIT - Added -1 to the first parameter of addFrameScript, because it is zero based (so to put a frameScript on frame 10 you would put 9).

TandemAdam
A: 

I figured it out. Honestly, I think my application is flawed. It worked miraculously and I didnt do anything. In my loop i did the if(currentframe == totalframes) and what didnt happen before is now happening perfectly. I dont know why. But like I said, it may be a software issue because I sometimes have issues passing values through contructors. And have to create special methods to accepts those values.

numerical25
A: 

it was because the object was getting called twice.

numerical25