I need a way to create a semi-realistic effect of a steady stream of smoke. I considered just making a quick animation with Flash’s build-in smoke effect in the deco tool, but the source of the smoke has to be able to rotate without moving the entire stream of smoke, therefore the stream has to be made up of multiple parts. So I figured I would continuously draw a ton of circles and have them grow larger and fade out over time. This looked great, and when the source needs to rotate, I simply have the circles to be created change their direction without changing the direction of the circles already created. I hope this makes sense. Anyway, this looks great and all, but I don’t think it’s too efficient. It has to be able to run really fast since it is being incorporated into a game. There will probably be no more than 5 sources at one time, but each stream of smoke continuously has 45 circles. I believe the code is just about optimized. But here it is anyway:
package projectiles {
import flash.display.Sprite;
import flash.events.Event;
import flash.filters.BlurFilter;
public class Smoke extends Sprite {
private var r:Object;
private var tarX:int;
private var tarY:int;
private var angle:Number;
private static var blur:BlurFilter=new BlurFilter(5,5,1);
private static var offset:Number=-.4;
private static var smokes:Array=[];
public function Smoke(X:int,Y:int,tar:Sprite) {
addEventListener(Event.ADDED_TO_STAGE, beginClass);
x=X;
y=Y;
tarX=tar.x;
tarY=tar.y;
filters=[blur];
if (int(Math.random()*2)==0) graphics.beginFill(0x27C638,.8);
else graphics.beginFill(0x23B835);
graphics.drawCircle(0,0,Math.random()*2+2);
graphics.endFill();
angle=Math.atan2(tarY-y,tarX-x);
angle+=offset;
offset+=.1;
if (offset>.4) offset=-.4;
}
private function beginClass(e:Event):void {
addEventListener(Event.ENTER_FRAME, eFrame);
removeEventListener(Event.ADDED_TO_STAGE, beginClass);
smokes.push(this);
}
private function eFrame(e:Event):void {
x+=Math.cos(angle)*2;
y+=Math.sin(angle)*2;
width+=.8;
height+=.8;
if (alpha<=0) removeMe();
else alpha-=.02;
}
private function removeMe():void {
removeEventListener(Event.ENTER_FRAME, eFrame);
parent.removeChild(this);
trace(smokes.length);
smokes.splice(smokes.indexOf(this),1);
}
}
}
I have all the instances of the Smoke class necessary created in pools and simply access them from there to cut down on creating new objects. I suppose I could also use an estimate rather than Math.sin/Math.cos, but I’m still worried this isn’t enough. Am I on the right track and does this just need some tweaking, or do I need a completely different approach? Thanks for the help