Ultrashock Forums > Flash > ActionScript
AS3 - Using Frame Label to Trigger Function
Member Blogs
 
Post Reply | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes

#1
Bookmark and Share!
AS3 - Using Frame Label to Trigger Function
Old 4 Weeks Ago

I making the transition to AS3 and am having trouble figuring this out. I have a animated movieclip (circle_mc) and the last frame is labeled "cover". How do I trigger a function when the frame with the label is reached?

This isn't working
ActionScript Code:
  1. circle_mc.addEventListener(Event.ENTER_FRAME, cover, false,0,true);
postbit arrow 4 comments | 290 views postbit arrow Reply: with Quote   
co2
Registered User
co2 is offline
seperator
Posts: 119
2002-02-06
Age: 35
co2 lives in United States
co2's Avatar
seperator

Ultrashock Member Comments:
Isocase Isocase is offline Isocase lives in United States 4 Weeks Ago #2 Old  
If I can remember correctly you cannot target a frame specifically by calling the frame label. I would just make a custom event class and then dispatch an even when you hit whatever frame/label you want. Here is an example, I made a quick custom event class and in the FLA but code on frame 1 and frame 30.

Code on frame 1:
Code:
import AnimationEvent;

addEventListener( AnimationEvent.FOO, foo, false, 0, true );

function foo( e:AnimationEvent ):void
{
	trace( "This method runs when the timeline hits frame 30" );
}
Code on frame 30:
Code:
stop();

dispatchEvent( new AnimationEvent( AnimationEvent.FOO ) );
Custom event class:
Code:
package
{
	import flash.events.Event;
	
	public class AnimationEvent extends Event
	{
		public static const FOO:String = "foo";
		
		public function AnimationEvent( type:String )
		{
			super( type );
		}
		
		override public function clone():Event
		{
			return new AnimationEvent( type );
		}
		
		override public function toString():String
		{
			return formatToString( "AnimationEvent", "bubbles", "cancelable", "eventPhase" );
		}
	}
}
Reply With Quote  
co2's Avatar co2 co2 is offline co2 lives in United States 4 Weeks Ago #3 Old  
Thanks for your help Isocase. Would an EventListener on the labeled frame work also?
Reply With Quote  
Isocase Isocase is offline Isocase lives in United States 4 Weeks Ago #4 Old  
You can add the event listener anywhere you want. You just have to make sure you dispatch the event so there is something to listen for.
Reply With Quote  
co2's Avatar co2 co2 is offline co2 lives in United States 4 Weeks Ago #5 Old  
Thanks again for your help. I was able to get it to work with this code on the main timeline.
ActionScript Code:
  1. circle_mc.addEventListener(Event.ENTER_FRAME, checkFrame);
  2.  
  3. function checkFrame(evt:Event):void {
  4.    if (circle_mc.currentLabel == "cover") {
  5.       circle_mc.removeEventListener(Event.ENTER_FRAME, checkFrame);
  6.       TweenMax.to(cover_mc, 2, { scaleX:.2, scaleY:.2, ease:Cubic.easeIn});
  7.    }
  8. }

Wohoo! 100th post!!!
Reply With Quote  
Thread Tools
Display Modes Rate This Thread
Rate This Thread: