Monday, December 21, 2009

Dynamically create and loop through MovieClip instances

Task: You need to dynamically create and then loop through MovieClips.
Solution: Use DisplayList and DisplayListContainer container methods.

var container:MovieClip = new MovieClip();
addChild(container);
var numItems:Number = 250;
var stageWidth:Number = stage.stageWidth;
var stageHeight:Number = stage.stageHeight;
function initClips():void
{
var c:MovieClip;
for(var i:Number = 0; i <>
{
c = new circle_mc();
randomizeClip(c);
container.addChild(c);
}
}
function randomizeClip(clip:MovieClip):void
{
clip.x = Math.random() * stageWidth;
clip.y = Math.random() * stageHeight;
clip.scaleX = Math.random() * 2;
clip.scaleY = Math.random() * 2;
var c:ColorTransform = new ColorTransform();
c.color = (Math.random() * 0xFFFFFF);
clip.transform.colorTransform = c;
clip.alpha = Math.random();
}
function onEnterFrame(event:Event):void
{
var c:MovieClip;
for(var i:Number = 0; i <>)
{
c = MovieClip(container.getChildAt(i));
randomizeClip(c);
}
}
initClips();
addEventListener(Event.ENTER_FRAME, onEnterFrame);


1 comment: