Old 26-03-2008, 21:34   #1 (permalink)
Hysteria
Registered User
 
Join Date: Feb 2008
Posts: 17
for loop question

I have a for loop that creates an amount of square sprites with random proprieties:
Code:
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)]; addChild(mySprite); }

anyone knows how I can delete all of these sprites at the same time?
  Reply With Quote
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
Old 27-03-2008, 14:19   #3 (permalink)
Hysteria
Registered User
 
Join Date: Feb 2008
Posts: 17
wow thanks! i'll be trying that right now
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8