Fundamentals of ActionScript 3.0: Develop and Design (Bruno Matricciano's Library) by Doug Winnie

Fundamentals of ActionScript 3.0: Develop and Design (Bruno Matricciano's Library) by Doug Winnie

Author:Doug Winnie
Language: eng
Format: epub
Publisher: Peachpit Press
Published: 2012-06-16T16:00:00+00:00


public function ManyCirclesWithLoops() {

_init();

}

private function _init():void {

circleCollection = new Array();

for (var i:uint = 0; i < 5; i++)

{

trace("Adding circle #" + i);

var tempCircle:BlueCircle = new BlueCircle();

tempCircle.x = 50 + (100*i);

tempCircle.y = 50;

tempCircle.addEventListener(MouseEvent.CLICK, _toggleVisible);

addChild(tempCircle);

circleCollection.push(tempCircle);

}

trace(circleCollection);

}

private function _toggleVisible(e:MouseEvent):void

{

if (e.target.alpha == 1)

{

e.target.alpha = .25;

} else {

e.target.alpha = 1;

}

trace ("Click on " + e.target + e.target.name);

}

}

}

In this example, an array called circleCollection is created for the purpose of holding the new BlueCircle instances. In the initialization function, you have a loop that will run five times. Each time you run the loop, you create a new “fresh” instance of the BlueCircle class, temporarily called tempCircle. You define its parameters, including a dynamic calculation of the x property based on the iterator variable, and add an event listener to the object. You then add the object to the display stack. Before the loop ends, you pass the instance to the circleCollection array using the push() method. This adds the instance to the array and then ends the loop.

After the last execution of the loop finishes, you output the contents of the array.

In the event callback function, you use the event object to toggle the alpha level of the object broadcasting the event. You send a confirmation message to the Output panel as well.

When you run the project, you’ll get something similar to what is shown in Figure 15.4.

Figure 15.4. Output from array example



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.