[COLOR=“DarkRed”]Hi,
This is a simple (simple?) particle system that came in the Learning ActionScript 3.0 book.
I was trying to create a particle system and than I thought could use this as a reference but I am lost.
I am confused, why is that without having numbers assigned to the variables _xpos, _ypos etc. it is working fine? Could someone explain this code, please?
Any help would be appreciated.
thanks,
fs_tigre[/COLOR]
package {
import flash.display.Sprite;
import flash.events.Event;
public class ParticleDemo extends Sprite {
public var emitterX:Number = stage.stageWidth/2;
public var emitterY:Number = stage.stageHeight/2;
public function ParticleDemo() {
stage.addEventListener(Event.ENTER_FRAME,onLoop, false, 0, true);
}
private function onLoop(evt:Event):void {
var p:Particle = new Particle(emitterX,
emitterY,
Math.random()*11 - 6,
Math.random()*-20,
1,
Math.random()*0xFFFFFF);
addChild(p);
}
}
}
[SIZE=“4”][COLOR=“Black”][COLOR=“DarkRed”]This is the particle classs[/COLOR][/COLOR][/SIZE]
package{
import flash.display.*;
import flash.geom.*
import flash.events.Event;
public class Particle extends Sprite {
private var _xpos:Number;
private var _ypos:Number;
private var _xvel:Number;
private var _yvel:Number;
private var _grav:Number;
public function Particle(xp:Number, yp:Number, xvel:Number, yvel:Number, grav:Number, col:uint) {
_xpos = xp;
_ypos = yp;
_xvel = xvel
_yvel = yvel
_grav = grav;
var ball:Sprite = new Ball();
addChild(ball);
x = _xpos;
y = _ypos;
alpha = .8;
scaleX = scaleY = Math.random() * 1.9 + .1;
var colorInfo:ColorTransform = ball.transform.colorTransform;
colorInfo.color = col;
ball.transform.colorTransform = colorInfo;
addEventListener(Event.ENTER_FRAME, onRun, false, 0, true);
}
private function onRun(evt:Event):void {
_yvel += _grav;
_xpos += _xvel;
_ypos += _yvel;
x = _xpos;
y = _ypos;
if (_xpos < 0 || _ypos < 0 || _xpos > stage.stageWidth || _ypos > stage.stageHeight) {
removeEventListener(Event.ENTER_FRAME, onRun);
parent.removeChild(this);
}
}
}
}