AS3: calling a function after a timeline animation is done


#1

I am developing a small application for android where i have to call a function after a timeline animation is done.

I tried using a timer and calling the function after a specific time(same as it takes the animations).

However as you all know that fps on air for android isn’t constant. So the timer isn’t accurate.


Please note that i am using classes and OOP.



In AS2 we used to call the function from the timeline (though i never done it, i used to work in codes only).

Is there any way in AS3.

Should i code all the animation, or is there an event that is triggered once a movieclip finishes ot riches a frame., any method
Thanks


#2

Just place the calling code at the end of the timeline, same as as2. I believe you need to set the function to public in order to work.


#3
Emroni said

Just place the calling code at the end of the timeline, same as as2. I believe you need to set the function to public in order to work.

I tried it but i keep getting an error.

the function is public and in the main class.

The movieclip is called to stage on a CLICK event, it does the animation and then must calls this function.

maybe its a target issue.
I’ll give it a retry


#4

Try this way. You don’t need to add code on the timeline:

playingMC.addEventListener(Event.ENTER_FRAME, checkTimeline);
function checkTimeline (e) {
	if (e.target.currentFrame == e.target.totalFrames) {
		e.target.stop();
		trace("Done. Call your function from here");
	}
}

#5
Emroni said

Just place the calling code at the end of the timeline, same as as2. I believe you need to set the function to public in order to work.

+1 Or you can dispatch an event if your animation is deeply nested

Last frame of animation:

stop();

dispatchEvent(new Event("finished", true));

Class:

public function SomeClass() {
			
	addEventListener("finished", done);
			
}
		
private function done(event:Event):void {
			
	trace("animation done");
			
}

#6
VF said

Try this way:

playingMC.addEventListener(Event.ENTER_FRAME, checkTimeline);
function checkTimeline (e) {
	if (e.target.currentFrame == e.target.totalFrames) {
		trace("Done. Call your function from here");
	}
}

Surprisingly it didn’t work !! currentFrame is reading zero and totalFrames is reading 1.

The target itself read the movieclip name.

Maybe i should try reading its child’s timeline or something. I’ll test it

@ Emroni : I fixed it and worked your method, But i’d rather keep all my code in my classes:D


#7
CrackerJack said
Emroni said

Just place the calling code at the end of the timeline, same as as2. I believe you need to set the function to public in order to work.

+1 Or you can dispatch an event if your animation is deeply nested

Last frame of animation:

stop();

dispatchEvent(new Event("finished", true));

Class:

public function SomeClass() {
			
	addEventListener("finished", done);
			
}
		
private function done(event:Event):void {
			
	trace("animation done");
			
}

Loved the dispatchEvent. A new thing to learn about AS3.

I am still new using it :smiley:


#8

Another question;
why doesnt this work:

addChild(mc);
mc.addEventListener(AnyEvent, removeMc);

private function removeMc(e:Event):void{			
			removeChild(e.target)
		}


#9

^ add the listener to “this” instead of mc


#10
CrackerJack said

^ add the listener to “this” instead of mc

It won’t work because i am dispatching an event once the movieClip animation is done.
then I call a function that removes the movieclip and does some stuff.

i even tried it with an ENTER_FRAME event, but i get an error must be a child…

edit: I could send the movieclip as a variable in the event .

ex:

mc.addEventListener("finish", function(){   newFunction(mc)  });

edit 2: It works but i hate that it must be that complex.


#11
SimplyDo said
CrackerJack said

^ add the listener to “this” instead of mc

It won’t work because i am dispatching an event once the movieClip animation is done.
then I call a function that removes the movieclip and does some stuff.

can you dispatch the event before you begin to clean up the animation assets?

-Jack


#12
SimplyDo said

Another question;
why doesnt this work:

addChild(mc);
mc.addEventListener(AnyEvent, removeMc);

private function removeMc(e:Event):void{			
			removeChild(e.target)
		}

Did you try generalizing a callback to ‘this’. For example create a general function in ‘this’

function del(targ:*){
this.removeChild(targ)
}

then use removeMC as you did but call the function

MovieClip(parent).del(e.target)

Could that help?
P


#13
patrickjansen said
SimplyDo said

Another question;
why doesnt this work:

addChild(mc);
mc.addEventListener(AnyEvent, removeMc);

private function removeMc(e:Event):void{			
			removeChild(e.target)
		}

Did you try generalizing a callback to ‘this’. For example create a general function in ‘this’

function del(targ:*){
this.removeChild(targ)
}

then use removeMC as you did but call the function

MovieClip(parent).del(e.target)

Could that help?
P

I will give it a try.



btw i got it working yesterday by simply sending the movieclip object as a variable.