Game Development Reference
In-Depth Information
We create a circle shape directly on the graphics attribute of the Sprite and fill it with the color
set previously. We clear its contents with the clear function call and set up the style of our line (2
pixels wide and white).
Next, we fill the circle with either red or blue (set in the switch:case statement earlier in this
function) with graphics.beginFill(shapeColor) . After calling beginFill , every shape we create
will be filled in with the shapeColor until we call endFill . So, we draw a circle at position 5,5 on
the Sprite with a radius of 8 , and end the fill.
graphics.drawCircle(5, 5, 8);
graphics.endFill();
Drawing dynamically is as simple as that. The rest of the init function goes like this:
1.
Randomly pick a starting location for the x and y attributes of the Circle (not the
shape we are drawing, but this Circle class instance as a whole).
2.
Set the starting scaleX and scaleY each to .5, the starting scale for all game circles.
3.
Set clicked to be false .
4.
Create a listener for the click.
5.
Set nextScale to equal the current scale at creation time.
Defining the update function for Circle.as
The update function is called from the SuperClick update function. As you have seen, the
SuperClick update function will iterate through all of the Circle instances on the game screen
and, in turn, call the Circle.update function on each. The Circle update function accepts a single
parameter called growSpeed .
public function update(growSpeed:Number):void {
if (fadingOut) {
alpha -= .05;
}else{
nextScale += growSpeed;
}
}
If the Circle instance fadingOut Boolean is set to true, the alpha value of the Circle instance is
decremented by .05. If the Circle instance is in the normal mode (not fading out but growing), the
nextScale attribute is incremented by the passed in growSpeed value.
Defining the dispose function for Circle.as
The dispose function gets rid of unwanted objects and readies them for Flash's built-in garbage
collection. It is called from the Game class's removeCircle function.
public function dispose():void {
removeEventListener(MouseEvent.MOUSE_DOWN, clickedListener);
}
Notice that we make sure remove all of our event listeners. By doing this, we free up all of the
unused objects for possible garbage collection.
Search WWH ::




Custom Search