View Single Post
Old 27-03-2008, 14:07   #2 (permalink)
theRemix
Battery Chicken
 
theRemix's Avatar
 
Join Date: Apr 2007
Location: Hawaii
Posts: 77
Send a message via AIM to theRemix Send a message via MSN to theRemix
yes, don't give a generic name like mySprite
i would put them in an array.
i would go a bit further and create a class (that extends Sprite) for your BasicDot.

the easy way:
Code:
var mySprites_ar:Array = new Array(); for (var i:Number=0; i<numBasicDots; i++) { var mySprite:Sprite = new Sprite(); mySprite.graphics.beginFill(colorArray[randRange(0,colorArray.length)], 1); mySprite.graphics.lineStyle(randRange(1,5), colorArray[randRange(0,colorArray.length)], 1, true) mySprite.graphics.drawRect(randRange(0,300), randRange(0,300), randRange(1,600), randRange(1,600)); mySprite.filters = filterArray[randRange(0,filterArray.length)]; mySprites_ar.push(addChild(mySprite)); }

this is really unpretty. we can do this because addChild returns a DisplayObject. so mySprites_ar contains all your sprites as DisplayObjects.

flash.display.DisplayObjectContainer (ActionScript 3.0)



the better way:
create a new class file for your BasicDot
Code:
package { import flash.display.Sprite; public class BasicDot extends Sprite { public function BasicDot() { super(); init(); } private function init(){ graphics.beginFill(colorArray[randRange(0,colorArray.length)], 1); graphics.lineStyle(randRange(1,5), colorArray[randRange(0,colorArray.length)], 1, true) graphics.drawRect(randRange(0,300), randRange(0,300), randRange(1,600), randRange(1,600)); filters = filterArray[randRange(0,filterArray.length)]; } } }

then in your original code
Code:
var mySprites_ar:Array = new Array(); for (var i:Number=0; i<numBasicDots; i++) { mySprites_ar.push(BasicDot(addChild(new BasicDot())); }
this will populate mySprites_ar with all your BasicDot objects


now in either case, you can loop through each one and remove them all like this:
Code:
for (var i:Number=0; i<mySprites_ar.length; i++) { removeChild(mySprites_ar[i]); }

this gives you much more flexibility and control

hth.
  Reply With Quote