Hey guys,
im trying to create a particlesystem where each particle moves by himself depending on which behavior has been assigned to him.
But… its slower than my grandma at the moment so I could really use some advice on how to make this structure a lot more faster!
One thing I would like to keep though is the ability to change the behavior of the particle later on.
Just have 2 classes (Particle & FlyAroundStage) at the moment and one that creates the swf.
package
{
import controlpanel.FPSCounter;
import flash.display.Sprite;
import flash.events.MouseEvent;
import particles.Particle;
import particles.behaviors.*;
[SWF(width="800",height="600",frameRate="60",backgroundColor="#e9e9e9")]
public class ParticleExpiriments extends Sprite
{
private var _particles:Array=new Array();
private var _fpsCounter:FPSCounter;
public function ParticleExpiriments()
{
addChild(_fpsCounter = new FPSCounter());
for (var i:int=0; i < 100; i++)
{
var x:int=Math.random() * stage.stageWidth;
var y:int=Math.random() * stage.stageHeight;
var color:uint=Math.random() * 0xFFFFFF;
var particle:Particle=new Particle(x, y, 5, color);
particle.behavior = new FlyAroundStage(particle);
_particles.push(particle);
stage.addChild(particle);
}
}
}
}
package particles
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.EventDispatcher;
import particles.behaviors.FlyAroundStage;
public class Particle extends Sprite
{
//particle appearance
private var _particle:Sprite;
private var _size:int;
private var _color:uint;
private var _filterArray:Array;
private var _behavior:FlyAroundStage;
public function Particle(x:int=0, y:int=0, size:int=5, color:uint=0x000000, cacheAsBitmap:Boolean=true)
{
this.x=x;
this.y=y;
_size=size;
_color=color;
this.cacheAsBitmap=cacheAsBitmap;
this.addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
}
private function init(evt:Event):void
{
this.removeEventListener(Event.ADDED_TO_STAGE, init);
drawParticle();
addChild(_particle);
}
//draws the particle
private function drawParticle():void
{
_particle=new Sprite();
_particle.graphics.beginFill(_color, 1);
_particle.graphics.drawCircle(0, 0, _size);
_particle.graphics.endFill();
}
//Getters & Setters
public function set behavior(behavior:FlyAroundStage):void
{
_behavior=behavior;
}
}
}
package particles.behaviors
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.utils.*;
import particles.Particle;
public class FlyAroundStage extends EventDispatcher
{
private var _particle:Particle;
private var _xVel:int;
private var _yVel:int;
private var _stageBorder:int;
private var _direction:int;
private var _speed:int;
private var _lastTime:int;
public function FlyAroundStage(particle:Particle, speed:int=5, direction:int=45)
{
_particle=particle;
_speed=speed;
_direction=direction;
particle.addEventListener(Event.ENTER_FRAME, onEnter, false, 0, true);
}
private function onEnter(evt:Event):void
{
var time:int=(getTimer() - _lastTime);
_lastTime=getTimer();
_xVel=Math.cos(deg2Rad(_direction)) * _speed;
_yVel=Math.sin(deg2Rad(_direction)) * _speed;
_particle.x+=(_xVel * time) / 100;
_particle.y+=(_yVel * time) / 100;
trace(time)
trace(_xVel);
}
//converts degrees to radians
private function deg2Rad(degrees:Number):Number
{
return degrees * (Math.PI / 180);
}
//converts radians to degrees
private function rad2Deg(radians:Number):Number
{
return radians * (180 / Math.PI);
}
//Getters & Setters
public function get speed():int
{
return _speed;
}
public function set speed(speed:int):void
{
_speed=speed;
}
public function get direction():int
{
return _direction;
}
public function set direction(direction:int):void
{
_direction=direction;
}
}
}
Its just flying down to the right. I want to make this way more sophisticated later on but first in need to get the structure right because right now, even though its just flying in a straight path, its way too slow.