Random Movement in AS3
- Page 2
by
kirupa | 24 December 2010
Have questions? Discuss this Flash / ActionScript
tutorial with others on the forums.
In the
previous page, you learned a little bit about what
it is that you will be seeing deconstructed. In this page,
you will hopefully learn a lot more about what it is that
you will be seeing deconstructed!
The first thing we are going to do is look at how the
circles make their way from your Library and into your
Stage. All of that is handled in the Main class, and this class lives in Main.as:
- package
- {
-
- import
flash.display.MovieClip;
-
- public
class
Main
extends
MovieClip
- {
-
- public
function
Main()
- {
- PopulateCircles();
- }
-
- private
function
PopulateCircles():void
- {
- for
(var
i:int=0;
i
<
50;
i++)
- {
- var
blueCircle:BlueCircle
=
new
BlueCircle();
-
- this.addChild(blueCircle);
- }
- }
- }
-
- }
The reason this class automatically runs when your
application loads is because this isn't a normal
class. It is our
document class which is associated with the FLA file:
[ where your Main class is being defined ]
If you don't know anything about what the document class
is, just note that it is what gets run automatically when
your application loads. The full tutorial (Document
Class) will give you more information on it.
The first thing that happens when your Main class gets
instantiated is that the constructor runs:
- public
function
Main()
- {
- PopulateCircles();
- }
The only thing our constructor does is call the
PopulateCircles function:
- private
function
PopulateCircles():void
- {
- for
(var
i:int=0;
i
< 50;
i++)
- {
- var
blueCircle:BlueCircle
=
new
BlueCircle();
-
- this.addChild(blueCircle);
- }
- }
As its name implies, the PopulateCircles function is
responsible for actually getting the circles displayed on
screen. That is done by using a for loop that creates the
BlueCircle objects and adds them to the root of our stage.
The BlueCircle class is nothing more than a movie clip of
a blue circle that I have stored in my Library:
[ where the BlueCircle lives ]
With the BlueCircle objects instantiated, what remains is
to have it be displayed on our stage. That is handled by the
following line:
- this.addChild(blueCircle);
Each time the code inside the loops runs, the above line line gets called,
and another BlueCircle
object is created and added for display on the stage.
Speaking of times the above line gets called, that is
entirely controlled by the for loop itself. In this case,
The loop simply goes from 0 to 49:
- private
function
PopulateCircles():void
- {
- for
(var
i:int=0;
i
< 50;
i++)
- {
- var
blueCircle:BlueCircle
=
new
BlueCircle();
-
- this.addChild(blueCircle);
- }
- }
This means that 50
circles are added to your application. You can adjust this
number up or down to change the number of circles that you
want to deal with. While having a large number of circles
may seem really cool, it can bring your animation to a crawl
on slower machines.
That is all there is to the Main class. Its main (ha!)
job is to get all of the BlueCircle objects added to your
stage. From there, each BlueCircle is responsible for itself. Let's look at how they are so responsible
starting on the
next page.
Onwards to the
next page!
|