ParticleBehavior = function(){} // create a particle behavior function ParticleBehavior.create = function(method, argumentsObject){ return ParticleBehavior[method].apply(null, ParticleBehavior.generateArguments(method, argumentsObject)); } // combine multiple particle behavior functions into one function ParticleBehavior.multiple = function(){ var i, a = arguments, n = a.length; return function(){ for(i=0;i= r){ this._x = r; vx = -Math.abs(vx); } if (this._y <= t){ this._y = t; vy = Math.abs(vy); }else if (this._y >= b){ this._y = b; vy = -Math.abs(vy); } } } ParticleBehavior.bounce.defaults = [ {param:"xvelocity", value:0}, {param:"yvelocity", value:0}, {param:"xincrease", value:0}, {param:"yincrease", value:0}, {param:"xfriction", value:1}, {param:"yfriction", value:1}, {param:"gravity", value:0}, {param:"bounds", value:{xMin: 0, yMin: 0, xMax: Stage.width, yMax: Stage.height}} ]; ParticleBehavior.bounce3D = function(x, y, z, vx, vy, vz, cx, cy, cz, fx, fy, fz, g, b, fl, w){ var l = b.xMin; var r = b.xMax; var f = b.zMax; var k = b.zMin; var t = b.yMax; b = b.yMin; if (g) cy -= g; var s; return function(){ x += vx = (vx + cx) * vx; y += vy = (vy + cy) * vy; z += vz = (vz + cz) * vz; if (x <= l){ x = l; vx = Math.abs(vx); }else if (x >= r){ x = r; vx = -Math.abs(vx); } if (y <= b){ y = b; vy = Math.abs(vy); }else if (y >= t){ y = t; vy = -Math.abs(vy); } if (z <= k){ z = k; vz = Math.abs(vz); }else if (z >= f){ z = f; vz = -Math.abs(vz); } s = fl/(fl + z); this._xscale = this._yscale = 100*s; this._x = x*s; this._y = -y*s; if (w) this.swapDepths(Math.round(s*1000)); } } ParticleBehavior.bounce3D.defaults = [ {param:"x", value:0}, {param:"y", value:0}, {param:"z", value:0}, {param:"xvelocity", value:0}, {param:"yvelocity", value:0}, {param:"zvelocity", value:0}, {param:"xincrease", value:0}, {param:"yincrease", value:0}, {param:"zincrease", value:0}, {param:"xfriction", value:0}, {param:"yfriction", value:0}, {param:"zfriction", value:0}, {param:"gravity", value:0}, {param:"bounds", value:{ xMin:Number.NEGATIVE_INFINITY, yMin:0, zMin:Number.NEGATIVE_INFINITY, xMax:Number.POSITIVE_INFINITY, yMax:Number.POSITIVE_INFINITY, zMax:Number.POSITIVE_INFINITY } }, {param:"focallength", value:300}, {param:"swap", value:false} ]; ParticleBehavior.size = function(xs, ys, c){ c = (c != "percent"); var r; return function(){ if (c){ if (r = this._rotation) this._rotation = 0; this._width += xs; this._height += ys; if (r) this._rotation = r; }else{ this._xscale += xs; this._yscale += ys; } } } ParticleBehavior.size.defaults = [ {param:"xincrease", value:0}, {param:"yincrease", value:0}, {param:"use", value:"percent"} /* "percent" or "value", "value" implied if not "percent" */ ]; ParticleBehavior.transparency = function(t){ var a; return function(){ if (a == undefined) a = this._alpha; this._alpha = a += t; } } ParticleBehavior.transparency.defaults = [ {param:"increase", value:0} ]; ParticleBehavior.repel = function (x, y, o, r, a, f) { var d,dx,dy,vx,vy,v; return function(){ if (x == undefined) x = this._x; if (y == undefined) y = this._y; if (o == Mouse){ dx = this._parent._xmouse - this._x; dy = this._parent._ymouse - this._y; }else{ dx = o._x - this._x; dy = o._y - this._y; } d = Math.sqrt(dx*dx + dy*dy); if (d < r) { var v = d*r*a; if (v != 0){ vx -= dx/v; vy -= dy/v; } } this._x += (vx = (vx + (x - this._x)*a)*f); this._y += (vy = (vy + (y - this._y)*a)*f); } } ParticleBehavior.repel.defaults = [ {param:"x", value:undefined}, {param:"y", value:undefined}, {param:"repeller", value:Mouse}, {param:"range", value:100}, {param:"acceleration", value:.03}, {param:"friction", value:.9} ]; ParticleBehavior.avoid = function(o, r, v, a, f){ var d,dx,dy,vx=0,vy=0,t,tv=0; return function(){ if (o == Mouse){ dx = this._parent._xmouse - this._x; dy = this._parent._ymouse - this._y; }else{ dx = o._x - this._x; dy = o._y - this._y; } d = Math.sqrt(dx*dx + dy*dy); if (d < r) { if (a && d) tv = Math.min(v, a*r/d); else tv = v; if (f) tv *= f; t = Math.atan2(dy, dx); vx = Math.cos(t); vy = Math.sin(t); }else if (f) tv *= f; else tv = 0; if (tv){ this._x -= vx*tv; this._y -= vy*tv; } } } ParticleBehavior.avoid.defaults = [ {param:"avoid", value:Mouse}, {param:"range", value:100}, {param:"velocity", value:1}, {param:"acceleration", value:0}, {param:"friction", value:0} ]; ParticleBehavior.constrain = function(b, c){ var l = b.xMin; var t = b.yMin; var r = b.xMax; b = b.yMax; c = (c != "bounds"); var pb; return function(){ if (c){ if (this._x < l) this._x = l; else if (this._x > r) this._x = r; if (this._y < t) this._y = t; else if (this._y > b) this._y = b; }else{ pb = this.getBounds(this._parent); if (pb.xMin < l) this._x = l - (pb.xMin - this._x); else if (pb.xMax > r) this._x = r - (pb.xMax - this._x); if (pb.yMin < t) this._y = t - (pb.yMin - this._y); else if (pb.yMax > b) this._y = b - (pb.yMax - this._y); } } } ParticleBehavior.constrain.defaults = [ {param:"bounds", value:{xMin: 0, yMin: 0, xMax: Stage.width, yMax: Stage.height}}, {param:"check", value:"bounds"} /* "bounds" or "center", "center" implied if not "bounds" */ ]; /* REMOVAL FUNCTIONS */ ParticleBehavior.boundsRemove = function(b, c){ var l = b.xMin; var t = b.yMin; var r = b.xMax; b = b.yMax; c = (c != "bounds"); var pb; return function(){ if (c){ if (this._x < l || this._x > r || this._y < t || this._y > b) this.removeMovieClip(); }else{ pb = this.getBounds(this._parent); if (pb.xMax < l || pb.xMin > r || pb.yMax < t || pb.yMin > b) this.removeMovieClip(); } } } ParticleBehavior.boundsRemove.defaults = [ {param:"bounds", value:{xMin: 0, yMin: 0, xMax: Stage.width, yMax: Stage.height}}, {param:"check", value:"bounds"} /* "bounds" or "center", "center" implied if not "bounds" */ ]; ParticleBehavior.timedRemove = function(t){ var s = getTimer(); return function(){ if ((getTimer()-s) >= t) this.removeMovieClip(); } } ParticleBehavior.timedRemove.defaults = [ {param:"time", value:1000} ]; ParticleBehavior.framesRemove = function(f){ var e = 0; return function(){ if ((++e) >= f) this.removeMovieClip(); } } ParticleBehavior.framesRemove.defaults = [ {param:"frames", value:100} ]; ParticleBehavior.changeInPositionRemove = function(d, c){ var d = d*d; var x,y,dx,dy; c = (c != "greater"); return function(){ if (x != undefined){ dx = x - this._x; dy = y - this._y; if (c){ if (d >= (dx*dx + dy*dy)) this.removeMovieClip(); }else{ if (d <= (dx*dx + dy*dy)) this.removeMovieClip(); } } x = this._x; y = this._y; } } ParticleBehavior.changeInPositionRemove.defaults = [ {param:"distance", value:10}, {param:"check", value:"greater"} /* "greater" or "less", "less" implied if not "greater" */ ]; ParticleBehavior.distanceRemove = function(x, y, d){ var d = d*d; var dx,dy; return function(){ if (x == undefined) x = this._x; if (y == undefined) y = this._y; dx = x - this._x; dy = y - this._y; if (d >= (dx*dx + dy*dy)) this.removeMovieClip(); } } ParticleBehavior.distanceRemove.defaults = [ {param:"x", value:undefined}, {param:"y", value:undefined}, {param:"distance", value:100} ]; ParticleBehavior.distanceTraveledRemove = function(d){ var d = d*d; var x,y,dx,dy,dt=0; return function(){ if (x != undefined){ dx = x - this._x; dy = y - this._y; dt += (dx*dx + dy*dy); if (d >= dt) this.removeMovieClip(); } x = this._x; y = this._y; } } ParticleBehavior.distanceTraveledRemove.defaults = [ {param:"distance", value:100} ]; ParticleBehavior.sizeRemove = function(nw, nh, xw, xh, c){ c = (c != "both"); var r; return function(){ if (r = this._rotation) this._rotation = 0; if (c){ if (nw && nw > this._width) this.removeMovieClip(); if (nh && nh > this._height) this.removeMovieClip(); if (xw && xw < this._width) this.removeMovieClip(); if (xh && xh < this._height) this.removeMovieClip(); }else{ if (nw && nh && nw > this._width && nh > this._height) this.removeMovieClip(); if (xw && xh && xw < this._width && xh < this._height) this.removeMovieClip(); } if (r) this._rotation = r; } } ParticleBehavior.sizeRemove.defaults = [ {param:"minwidth", value:1}, {param:"minheight", value:1}, {param:"maxwidth", value:undefined}, {param:"maxheight", value:undefined}, {param:"check", value:"both"} /* "both" or "single", "single" implied if not "both" */ ];