AS3 DocumentClass in Plain English
This tutorial is for those that want to jump into the Action Script 3.0 bandwagon but don’t know where to begin with. Let’s begin with writing a document class some people refer it as the “package thingy”.
Download the tutorial materials here.
Unzip to your desktop and open the .as (ActionScript) and .fla file. Note: These are flash CS3 files so, best to have CS3 with you.
Take a look at the picture beside you, now in the future, you will be writing most of the Action Script externally, rather than rely heavily inside the fla.
There’s a few reason why Adobe is adopting this strategy and you will have to get your hands really dirty to appreciate why they are doing this.
Here is one of the few reasons :-
- Old action script scattered over the fla files (some action script are scripted on top a movie / button, some are on stages) are hard to debug when some one else changes it. Working in a large team all sharing the same fla is confusing.
- There’s a limitation in controlling objects in the old Action Script, objects need to be loaded first (either via linking in library or etc) before you can script control it. This limitation is overcome in Flash 8 in Action Script 2.0 but, still there’s always a limit and written prototype and components is more complicated compared to writing an Action Script 3.0 document class.
- Too much object in fla and in library, makes the file way too big (big chunk). Action Script 3.0 introduce a few lightweight tips and tricks.
- Older components and compiled clips are heavy on the loading.
Ok, enough of those theory stuff, let’s start the tutorial shall we. We shall start with how we are going to include a documentclass. First just create a plain folder to store both your working fla as well as the actionscript file both in the same folder. In this tutorial you are going to learn how to,
- Create a document class and attach it to the fla.
- Learn how objects are added into the stage in Action Script 3.
- Use the document class to create a movieclip object.
- The movieclip object will then have another text object (nested).
- Initialize! Get the package thingy running!.
First start you new fla. Make sure is Action Script 3.0 file. See pictures below.
Figure 1.1 : Select Flash File (ActionScript 3.0)
Figure 1.2 : Head towards the stage properties below, this is where to associate your document class actionscript file to your fla.
Figure 1.3 : You can also associate it in Publish Settings.
Just like in Figure 1.2, let’s type in our “about to be document class” as myDocClass. Your next step is to create another New File this time is a pure Action Script 3.0.
This file need to be saved in the same folder named myDocClass.as must be the same name as declared in your fla to link up. You will need to write some ActionScript 3 stuff in it. Before you start look at the picture below, to understand the package class structure.
Figure 3.1 : The Package Thingy!
A document class start with a package, followed by libraries of stuff to import. The libraries of stuff to import is to tell the compiler what needed to be loaded to accomplish certain task. After that, you declare your public class myDocClass (which matches your document class name in fla. This is followed by the declaration of variables to use in this class. These variables can be made private and public.
So what’s with the private and public thingy? See diagram below. Basically, by declaring variables and function in public (in the brown bridge), means you can access them and execute them from the stage (main fla) itself. It can be from a button from stage or whatever. Private ones are restricted areas reserved and used within the package itself.
Well, that’s it for the basic fundamental principles. The rest is to look at the downloaded materials (open up the sample_tutorial.fla and myDocClass.as) for more practical (check the comments left there). Go ahead tweak the x, y values, try and explore by modifying the codes to do more. I’ll be posting more in the future so stay tuned ;).