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:

  1. frame in the timeline
  2. 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):

package
{
 import flash.display.MovieClip;
 
 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.

Advertisements

~ by brianchau on December 1, 2007.

5 Responses to “Where to put Actionscript 3 codes in Flash CS3 application?”

  1. If you use a decent editor like Eclipse + FDT or Flex Builder all imports would be automatic.

    The link below might be of some interest to you, although a little off subject.
    http://www.tink.ws/blog/create-classes-command-panel/

  2. Also if you make a template file that you pull from w/ new files that has all of the general packages you’d need it will save time.

    package
    {
    import flash.display.*;
    import flash.events.*;
    //etc

    public class Template extends MovieClip
    {
    public function Template():void
    {

    }
    }
    }

  3. Bully Blackadder!

  4. nice post!

    bye!

  5. WOW. O.o this shit is hard 😛

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: