Help... why do these movie clips hang around??
I was trying to create a general class for particles, but some of the particles hang around
for some unknown reason. Any suggestions on what could be happening?
here's the code from the fla:
stop();
var wide = 400;
var high = 300;
var timer =5;
var foglayer = new Sprite();
var blur = new BlurFilter(10,6);
addChild(foglayer);
foglayer.filters = [blur];
for (var i=0; i<100; i++) {
addParticle();
}
function addParticle() {
var ww = wide /2;
var hh = high /2;
// stars splashing up
var particle = new Particle(Spark,this,ww,high);
particle.xv = randRange(-5,5);
particle.yv = randRange(-10,0);
particle.clip.scaleX = randRange(.5,1);
particle.clip.scaleY = particle.clip.scaleX;
particle.fade = .98;
particle.grav = .1;
// smoke coming from mouse
particle = new Particle(Cloud,foglayer,mouseX,mouseY);
particle.xv = randRange(-1,0);
particle.yv = -3;
particle.shrink = 1.01;
particle.spin = randRange(1,3);
particle.fade = .99;
}
function randRange(min:Number,max:Number) {
return Math.random() * (max - min) + min;
}
function Update(e) {
addParticle();
count.text = this.numChildren+"";
}
addEventListener(Event.ENTER_FRAME,Update);
And here from the Particle class:
package {
import flash.display.*;
import flash.events.*;
public class Particle extends MovieClip {
public var clip:DisplayObject;
public var wide:Number = 400;
public var high:Number = 300;
public var xv:Number = 0;
public var yv:Number = 0;
public var fade:Number = 1;
public var drag:Number = 1;
public var grav:Number = 0;
public var xs:Number =0;
public var ys:Number =0;
public var oxv = 0;
public var oyv =0;
public var shrink = 1;
public var spin = 0;
public var clock =0;
public function Particle ( symbol:Class, target:DisplayObjectContainer,xpos:Number, ypos:Number) {
clip = new symbol();
target.addChild(clip);
clip.x = xs = xpos;
clip.y = ys = ypos;
oxv = xv;
oyv = yv;
addEventListener(Event.ENTER_FRAME,update);
}
public function init(){
clip.alpha = 1;
clip.x = xs;
clip.y = ys;
xv = oxv;
yv = oyv;
}
public function update(e) {
clip.x += xv;
clip.y += yv;
xv *= drag;
yv *= drag;
yv += grav;
clip.scaleX*=shrink;
clip.scaleY*=shrink;
clip.alpha *= fade;
clip.rotation += spin;
clock++;
if (offScreen()||clip.alpha <.1 || clock > 1000){ kill() }
}
public function kill() {
this.removeEventListener(Event.ENTER_FRAME,update);
this.clip.parent.removeChild(clip);
//this.parent.removeChild(this);
}
public function offScreen():Boolean {
var xx = clip.x;
var yy = clip.y;
var ww = clip.width/2;
var hh = clip.height/2;
if (xx > wide + ww ||xx< -ww ||yy< -hh ||yy>high+hh) {
return true;
} else {
return false;
}
}
}
}
I think it might have to do with the function kill() but I am not sure.