Questa classe permette di creare un effetto fumo davvero realistico. Nella directory dov’è localizzato il file .fla create una nuova cartella e rinominatela “script”. Adesso aprite un editor di testo qualsiasi e copiate e incollate il seguente codice:
/*
*The smoke effect is achieved by creating small circles
*They are continuously moved upwards
*A BlurFilter is applied to make them look realistic
*As they float above their dimensions are increased and the alpha reduced
*Actually only 40 particles are present and they are continuosly
recycled to prevent the main memory occupied by the .swf file
from increasing.
*/
package script{
import flash.display.Sprite;
import flash.display.Shape;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.filters.BlurFilter;
import flash.utils.Timer;
public class Smoke extends Sprite
{
/*this will be the parent container*/
private var smoke_mc:MovieClip = new MovieClip();
/*color of the smoke*/
private var color:uint;
/*radius of each smoke particle*/
private var partRadius:Number = 1;
/*BlurFilter for the smoke particles*/
private var blur:BlurFilter = new BlurFilter(8,8,2);
/*timer to attach smoke particles*/
private var timer:Timer = new Timer(100);
/*
the constructor takes 3 parameters-
X and Y coordinates of the point of origination
and the color of the smoke
*/
public function Smoke(xc:Number,yc:Number,col:uint)
{
/*attach the parent movieClip*/
smoke_mc.x=xc;
smoke_mc.y=yc;
addChild(smoke_mc);
/*assign the color*/
color=col;
/*start the timer*/
timer.start();
/*listener for the timer. the function startSmoke
is called every 100 miliseconds*/
timer.addEventListener(TimerEvent.TIMER,startSmoke);
}
/*attaches one smoke particle to the stage*/
private function startSmoke(e:TimerEvent)
{
/*if 40 particles has been attached
stop attaching any more */
if(smoke_mc.numChildren>40)
{
/*stop the timer*/
timer.stop();
/*remove the timer listener*/
timer.removeEventListener(TimerEvent.TIMER,startSmoke);
}
/*atach a particle to the parent movieclip*/
smoke_mc.addChild(smokeParticle());
}
/*Create and return a fully formed smoke particle*/
private function smokeParticle():Sprite
{
var smokePart:Sprite = new Sprite();
smokePart.graphics.beginFill(color);
/*create the particle at a random point
between +2 and -2 pixels from the given
X coordinate*/
var randX = -2+Math.random()*4;
smokePart.graphics.drawCircle(randX,0,partRadius);
smokePart.graphics.endFill();
/*blur the particle*/
smokePart.filters=[blur];
/*add more randomness to the X coordinate*/
var randDist = -10+Math.random()*20;
smokePart.x+=randDist;
/*move the particles*/
smokePart.addEventListener(Event.ENTER_FRAME,moveUp);
return smokePart;
}
/*
move and resize the particles
change the values to see their
effects on the smoke
*/
private function moveUp(e:Event)
{
/*if the particle is visible*/
if(e.currentTarget.alpha>0)
{
/*move it upwards*/
e.currentTarget.y-=1;
/*increase the width*/
e.currentTarget.width+=0.5;
/*incraese the height at a lesser rate*/
e.currentTarget.height+=0.3;
/*reduce the alpha*/
e.currentTarget.alpha-=.004;
}
/*if the particle is not visible
restore Y to 0 ,X to a random
value, width and height to the
original radius and the alpha to 1*/
else
{
e.currentTarget.y=0;
var randDist = -5+Math.random()*10;
e.currentTarget.x+=randDist;
e.currentTarget.width=partRadius;
e.currentTarget.height=partRadius;
e.currentTarget.alpha=1;
}
}
}
}
Adesso salvate il file col nome “Smoke.as”, aprite il vostro file flash e premete Ctrl+J, impostate la larghezza e l’altezza a 200 x 300, premete F9 e nella finestra delle azioni copiate e incollate il seguente codice:
/*import the custom class*/
import script.Smoke;
/*call the constructor to create a new instance of the Smoke class
change the X and Y coordinates and the Color of the smoke here.
*/
var smoke:Smoke=new Smoke(100,150,0xBBBBBB);
/*add the instance to the stage*/
addChild(smoke);
fonte: www.sastgroup.com » Vai al post originale