Archive

Posts Tagged ‘object-oriented’

How to Write a Custom Class

May 26th, 2009
Get Adobe Flash player

Using Custom Classes is an introduction to Object-Oriented Programming, which is beyond the horizon of this class.

If its something you’re interested in, here’s something to get you started. The most important thing to take away from this is that we are going to write the functionality for what we want, say a ball, to do, and no matter how many balls we create, they will all inherit from a single source of logic, a Ball Class.

When we say Class, we me an external ActionScript file that follows the rules needed to make it a class. Any text file saved with a .as extension can be used as an ActionScript file.

We’re going to create a library item with 4 frame labels. We’ll also create a Custom Class that each instance of the Ball MovieClip will inherit from. This custom class will add listeners for MouseEvents, so that all of our ball instances do the same thing, and run off of the same code.

Download Example

Follow this example

Step 1 - Create the Library Item

Open a new .fla, make sure you have ActionScript 3 selected, and create a ball MovieClip. Inside the ball MovieClip, create a labels layer and an actions layer. Place a stop() Action on frame 1 of the Ball MovieClip. In the labels layer, make frames 1 - 4 blank keyframes. Label one frame “default”, one frame “over”, one frame “out”, and one frame “down”. Change something visually on each frame so you can tell if the MouseEvents are working. Save the .fla as “ExportForActionScript.fla

Step 2 - Relate the Library Item to the External class

Choose File > New > ActionScript File. Save the ActionScript file in the same directory as the ExportForActionScript.fla. Paste this code in the ActionScript file and save it.

If all goes well you won’t get any errors and you should notice that they MouseEvents are working. If not, compare the working example you can download here with what you have.