External BubbleClass file

We end this tutorial with a method that is equivalent to that shown on the previous page. For a movieclip that is linked for use with ActionScript we can attach the script to the symbol as an external .as file. We will present instructions for accomplishing this from the FreeBubbles.fla file presented on the previous page. There is a source file BubbleClassDemo.fla with a final version available from the Download section at the bottom of this page.

Getting your file ready

Rename the file equivalent to the FreeBubbles.fla file created on the previous page. We will refer to it as BubbleClassDemo.fla. Open the Library panel (Ctrl+L), select the BubbleClip movieclip and right-click on it to select Edit from the pop-up menu. Open the Actions panel within the BubbleClip timeline and delete the entire script from the timeline. Click Scene 1 to return to the Main Timeline.

Once again find the BubbleClip clip in the Library panel and right-click again, but this time select Rename from the pop-up menu and give this class the new name BubbleClass instead of BubbleClip. You will need to right-click it one more time to choose Linkage so that you can make the corresponding Class name change to BubbleClass there as well.

You now have a library movieclip called BubbleClass whose linkage property says that it is attached to the Class named BubbleClass, but there is no script attached to BubbleClass. If you Control > Test Movie at this point you will see nothing happen because there are lots of bubbles being created outside of the mask area, and they will never move up and never remove themselves. (Don't let it run too long!)

We will next create a class file that will be attached to the movieclip symbol when the application is compiled.

Creating the file BubbleClass.as

In the Flash authoring environment, choose File > New and select ActionScript File from the list of choices for "Type." You will have only the Actions panel available to you. Type the following code and name the file BubbleClass.as in the same directory as the BubbleClassDemo.fla file you created above.

Note that this file is available in the Download section at the bottom of this page if you don't feel like typing it. Also, it is ok to edit this file in any text editor instead of within Flash.

package {

import flash.display.MovieClip;

import flash.events.Event;

public class BubbleClass extends MovieClip {

public function BubbleClass() {

this.addEventListener(Event.ENTER_FRAME, moveMe);

}

private function moveMe(evt:Event):void {

this.y -= this.width/2;

if (this.y < -100) {

this.removeEventListener(Event.ENTER_FRAME, moveMe);

this.parent.removeChild(this);

}

}

}

}

Note that this code is identical to the code we placed on the BubbleClip timeline. It is just organized slightly differently to be consistent with the package/class organization discussed elsewhere in this section. Now if you Control > Test Movie, you will see the exact same behavior as on the previous page.

This approach allows for easier access to class properties and behavior than the method of adding timelines within timelines in the Flash authoring system, so it is desirable especially for more complex applications.

Download

Back to Bridging the Gap             Back to Flash and Math Home

The site www.flashandmath.com is maintained by Doug Ensley (doug@flashandmath.com) and Barbara Kaskosz (barbara@flashandmath.com).
It has been developed with partial funding from the National Science Foundation and the Mathematical Association of America.