Add a frame script (addFrameScript) dynamically in Flash CS3

It is been around for some time but not so many people actually know already this little undocumented feature in Flash CS3. You can actually add frame scripts dynamically by using the addFrameScript() method. Imagine you want to add some script to the timeline of a movieclip myStar: you can actually do this from the document class like this short code example shows:

package com.newmovieclip{
   import flash.display.MovieClip;
   public class Main extends MovieClip {
   public var myStar:Star;
             //constructor
	public function Main() {
		//place a star on stage
		myStar = new Star();
		addChild(myStar);
		//add script to star timeline on frame 5
		myStar.addFrameScript(4,frameFunction); // (zero based)
	}
	private function frameFunction():void {
		trace("Executing  code for star Frame  5" );
		//delete frame script by passing null as second parameter
		myStar.addFrameScript(4,null);
            }
}
}

As you see we can pass the framenumber and the function to be executed when the playhead enters that frame. Sometimes this can be very handy I think. At least to keep all your code in one place as it is a best practice 🙂
You can also see that when you do not want the function to be registered with that specific frame that you can use the same method but just pass null as the second parameter .

So hope this helps you 😉

9 responses to “Add a frame script (addFrameScript) dynamically in Flash CS3

  1. Franky Natividad 6 May , 2007 at 10:02 am

    Awsome mates 🙂 Thanks Ill use this on my new api to make it cooler 🙂 Making one so as3 becomes alot easier with movie clips at least and keyboard events

  2. Koen 6 May , 2007 at 10:10 am

    @ franky : At least with keyboard events 🙂

  3. Franky Natividad 9 May , 2007 at 7:58 pm

    What you mean ? It has movie clip and key board access plus Im going to add xml socket suppot

  4. bestgold 7 October , 2007 at 1:53 pm

    Welcome to my site!
    My site describes kinds of earnings by means of sponsors!
    Thanks!

  5. compelo 16 April , 2008 at 8:05 am

    Why do they NOT make it all official? man. those guys tick me off really bad! It’s like working with Microsoft documentation

  6. Rolando Rasmussen 18 April , 2008 at 1:26 pm

    tribromophenol somacule cudgeler neronian pretry postspasmodic zwinglian skyman
    Big L’s Paintball Plus
    http://www.trincoll.edu/orgs/praxis/

  7. Pingback: Undocumented add frame scripts method AS3 « NiK oN FlAsh

  8. Pingback: Undocumented add frame scripts method AS3 « Domy Incorporation

  9. Pingback: addFrameScript and currentLabels | Flash Monkey