Where to put Actionscript 3 codes in Flash CS3 application?
I have been presenting at some eSeminars on Actionscript 3 and always got asked where should we put the codes in a Flash CS3 movie. As of AS3, we can no longer put codes right onto the movieclip or button object itself. I for one wouldn’t miss this as putting codes everywhere can be quite messy. So that leaves 2 other places to put AS3 codes:
- frame in the timeline
- external file (.as) packaged as a document class file
Personally I like the 2nd approach better. It totally separates codes from the main movie. The downside for me is that I would have to create a separate as file (called it Main.as for example and set that as the document class) and have to type the following codes manually all the time (assuming Main.as and the flash file .fla are at the same folder):
public class Main extends MovieClip
public function Main():void
stop(); // if I need to stop the movie at frame 1
As you can see from the above codes, I also have to import the movieclip class as well before I can call the stop() method. I wish AS3 would be a bit more intelligent and would automatically import whatever class that is required to compile the codes. It is a bit of a pain but I would soldier on. With a bit of practice, I would get the hang of it no doubt.