PDA

View Full Version : remove child from event listener



RebuiltJorge
May 25th, 2008, 10:05 AM
ok I have a game that at a certain point starts to omit particles, I give them motion through an event listener called moveParticles, however whenever I try to remove them from the display list, my program crashes,

ActionScript Code:

//EMITS PARTICLES
public function addParticles(x,y){
var particle:MovieClip;

for(var i:int = 0; i < 200; i++){
particle = new MovieClip();
randomParticleX = Math.random() * 60 - 30;
randomParticleY = Math.random() * 60 - 30;
particle.movementX = randomParticleX;
particle.movementY = randomParticleY;
particle.x = x;
particle.y = y;

particle.graphics.lineStyle(1);
particle.graphics.beginFill(0xFFFFFF);
particle.graphics.drawCircle(0,0,2);
particle.graphics.endFill();
particle.addEventListener(Event.ENTER_FRAME,movePa rticles);
particles.push(particle);
addChild(particle);

}
}
public function moveParticles(evt:Event){
var thisParticle = (evt.target as MovieClip);
thisParticle.x += thisParticle.movementX;
thisParticle.y += thisParticle.movementY;
if(evt.target.x >= 1000 || evt.target.x <= 0 || evt.target.y >= 900 || evt.target.y <=0){
removeChild(thisParticle);
}
}

Felixz
May 25th, 2008, 10:59 AM
removeChild(thisParticle);
thisParticle.removeEventListener(Event.ENTER_FRAME ,moveParticles);Instead MovieClip u can extend Shape class to have movementX and movementY properties

RebuiltJorge
May 25th, 2008, 12:44 PM
wow, I totally forgot about the eventListeners, thanks Felixz