PDA

View Full Version : Smoking out AS2 for AS3



mike-in-drag
August 27th, 2008, 08:19 PM
I've been looking for a fairly decent smoke tutorial for a while and did come across this one:

http://www.pixelhivedesign.com/tutorials/Realistic+Flash+Smoke+Effect/

however, this didn't really help as i want to do all of my coding in AS3. so i went out to convert this (AS2):


fadeSpeed = 1;
floatUpSpeed = 2;
this.onEnterFrame = function ()
{
d = this.getNextHighestDepth();
aPuff = attachMovie("aPuff", "aPuff" + d, d);
aPuff._xscale = aPuff._yscale = 10;
aPuff._x = Math.random() * 5;
aPuff.gotoAndPlay(Math.round(Math.random() * 20));
aPuff.onEnterFrame = function ()
{
this._xscale = this._yscale = this._yscale + fadeSpeed;
this._alpha = this._alpha - fadeSpeed;
this._y = this._y - floatUpSpeed;
if (this._xscale >= 100)
{
this.removeMovieClip();
} // end if
};
};

Now after attempting to convert this to AS3 i came up with this:


var fadeSpeed:Number = new Number(1);
var floatUpSpeed:Number = new Number(2);
var puff:MovieClip = new aPuff();

this.addEventListener(Event.ENTER_FRAME, smokeStart, false, 0, true);

function smokeStart(evt:Event):void{

addChild(puff);
puff.scaleX = puff.scaleY = 10;
puff.x = Math.random() * 5;
puff.gotoAndPlay(Math.round(Math.random() * 20));
puff.addEventListener(Event.ENTER_FRAME, moveSmoke, false, 0, true);
function moveSmoke(evt:Event):void{
this.scaleX = this.scaleY = this.scaleY + fadeSpeed;
this.alpha = this.alpha - fadeSpeed;
this.y = this.y - floatUpSpeed;
if (this.scaleX >= 100);
{
puff.removeEventListener(Event.ENTER_FRAME, moveSmoke);
}
}
}

Now i get to see the smoke, but not in the same way, just like this (http://www.cynical-forum.com/media/gunsmoke.swf). Can someone take a brief glimpse over what i have done and give me a pointer as to where i may have gone wrong.



Thanks in advance, Mike.

mike-in-drag
August 27th, 2008, 10:27 PM
I did just notice the var's at line 1 and 2 were written incorrectly and have now rectified that. I've also rectified the initial puff scaling as the integer was incorrect, that should have been 0.1.

However going through the rest of the code, as far as i can gather, the problem seems to come from frame 13 onwards:


puff.addEventListener(Event.ENTER_FRAME, moveSmoke);

tckober
August 28th, 2008, 02:00 AM
also keep in mind that scaleX and scaleY (as well as alpha), are using values between 0 and 1.

http://www.senocular.com/flash/tutorials/as3withflashcs3/?page=2

mike-in-drag
August 28th, 2008, 03:05 AM
Thankyou.

Done, but unfortunately still no go. Currently my code looks like this, but still doesn't work:


var fadeSpeed:Number = 0.01;
var floatUpSpeed:Number = 2;
var puff:MovieClip = new aPuff();

this.addEventListener(Event.ENTER_FRAME, smokeStart);

function smokeStart(evt:Event):void{

addChild(puff);
puff.scaleX = puff.scaleY = 0.1;
puff.x = Math.random() * 5;
puff.gotoAndPlay(Math.round(Math.random() * 2));
puff.addEventListener(Event.ENTER_FRAME, moveSmoke);
function moveSmoke(evt:Event):void{
this.scaleX = this.scaleY = this.scaleY + fadeSpeed;
this.alpha = this.alpha - fadeSpeed;
this.y = this.y - floatUpSpeed;
if (this.scaleX >= 1);
{
puff.removeEventListener(Event.ENTER_FRAME, moveSmoke);
}
}
}

but still nothing after the second addEventListener seems to want to co-operate:

http://www.cynical-forum.com/media/gunsmokeAS3.swf

sekasi
August 28th, 2008, 03:37 AM
test this out .. draw a lil smokepuff and make it a symbol (center registration point). export for actionscript, call it aPuff.


var fadeSpeed:Number = 0.01;
var floatUpSpeed:Number = 2;
var puff:Sprite;
var tmp:Sprite;

this.addEventListener(Event.ENTER_FRAME, smokeStart);

function smokeStart(evt:Event):void{
puff = new aPuff();
addChild(puff);
puff.scaleX = puff.scaleY = 0.1;
puff.x = (stage.stageWidth / 2) - 20 + (Math.random() * 40)
puff.y = stage.stageHeight;
puff.rotation = int(Math.random() * 360);
for (var i:int = 0; i < numChildren - 1; i++) {
tmp = Sprite(getChildAt(i));
tmp.scaleX = tmp.scaleY = tmp.scaleX + fadeSpeed;
tmp.alpha -= fadeSpeed;
tmp.y -= floatUpSpeed;
tmp.rotation += 0.5;
if (tmp.scaleX >= 1) removeChildAt(i);
};
}


Just translated your code a bit. :) *shrug*

mike-in-drag
August 30th, 2008, 02:59 PM
Works a treat, thank you very much. Now time to tweak it here and there.