Removing particles in AS3
- Started
- Last post
- 7 Responses
- ok_not_ok
Hey out a noob here..tanks
I was given this code that generates a particles:
ar particleArray:Array = new Array();
var maxParticles:Number=50;function addParticle(e:Event) {
var dot:Particle = new Particle();
dot.x=stage.stageWidth/2;
dot.y=stage.stageHeight/2;
//dot.alpha=Math.random()*.8+.2;
dot.scaleX=dot.scaleY=Math.rando...
dot.xMovement=Math.random()*20-5...
dot.yMovement=Math.random()*20-5...
particleArray.push(dot);
addChild(dot);
dot.cacheAsBitmap=true;
if (particleArray.length>=maxPartic... {
removeChild(particleArray.shift...
}
dot.addEventListener(Event.ENTER...}
function moveParticle(e:Event) {
e.currentTarget.x+=e.currentTarg...
e.currentTarget.y+=e.currentTarg...
}var myTimer:Timer=new Timer(30);
myTimer.addEventListener(TimerEv... addParticle);/==
Now, let's say Im done with the code. How do I remove the particles that's on the screen?
Tanks!
- Milan0
dude, if you don't know how to do something as basic as removing a object from the display list, you should not be anywhere near particle-generation code. you'll end up with a flash app that leaks memory and crashes browsers.
i would start here:
- fyoucher10
I would add the particles to a new sprite, and then whenever you need to delete all of the particles, just delete the sprite. This is like nesting the particles in its own container.
Or if you don't want to do that, you can give the particles generated an instance name (i.e. dot1, dot2), then loop through all of the objects in the display list and search for clips with the characters "dot" in the instance name and then remove them.
- ifeltdave0
particle.GTFO = true;
- acrossthesea0
Somewhere in that code has to be something like: myTimer.start();
Find where that is and comment it out. If the timer never goes off, then it shouldn't keep creating particles, right?
- ok_not_ok0
i have: myTime.stop();
But sometimes a pesky single particle likes to linger in the middle and I just wanna remove that.
- Oh, you want to show particles then remove them?acrossthesea
- acrossthesea0
Looks like you can loop through that array called particleArray. Something like this might work:
function releaseTheKraken(){
for(var i=0;i<particleArray;i++){
removeChild(particleArray[i]);
trace("removing particle # " + i);
}
}- releaseTheKraken();acrossthesea
- shoot, meant to say: for(var i=0;i<particleArray...
acrossthesea - particleArray.lengthacrossthesea
- THANKS!ok_not_ok