Do you like things? Me too. I also like to think about things and have ideas. Come join me and see what I come up with.
//Main.as package { import flash.display.MovieClip; public class Main extends MovieClip { public function Main() { } } }
//Main.as internal const SWIDTH:Number = 480; internal const SHEIGHT:Number = 480;
//Main.as //this will instantiate 80 fire particles and that's it. //We'll use the "FireParticle.as" (which we haven't created yet) //to get them to reinitialize themselves. for (var i:int = 0; i < 80; i++) { //creates a new FireParticle at the index above our fire so it's always on top. addChildAt(new FireParticle(), getChildIndex(fire) + 1); }
//FireParticle.as package { import flash.display.MovieClip; public class FireParticle extends MovieClip { //MEMBERS private var ROOT:Object;//our "Main.as" is our root so we need to access it somehow private var ySpeed:Number;//our particles have to move. What better way than giving them some speed? //METHODS public function FireParticle() { addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true); } private function init(e:Event = null) : void//we set this to "null" so we can use reinitialize it later { } } }
//FireParticle.as ROOT = root;//get our root MovieClip alpha = Math.random(); x = ROOT.fire.x;//sets the x in the center of our fire y = ROOT.fire.y;//sets the y in the center or our fire ySpeed = 4;//will be used in our ENTER_FRAME listener addEventListener(Event.ENTER_FRAME, update, false, 0, true);
//FireParticle.as private function update(e:Event) : void { y -= ySpeed;//moves this particle toward the top of the screen alpha -= 0.01;//lowers this particle's alpha every frame if (alpha <= 0)//if this particle becomes invisile... { init();//...we want to reset it //this is why we set our "init()" function to a default of "null" } }
//FireParticle.as private function randomRange(minNum:Number, maxNum:Number) : Number { return Math.floor((Math.random() * (maxNum - minNum + 1)) + minNum); }
//EXAMPLE private function randomRange(minNum:Number, maxNum:Number) : Number { return Math.floor((0.2 * (5 - 3 + 1)) + 3); }
//FireParticle.as ROOT = root;//get our root MovieClip alpha = Math.random(); x = randomRange(ROOT.fire.x - ROOT.fire.width / 2, ROOT.fire.x + ROOT.fire.width / 2);//this makes our particle's x initialize somewhere within the width of our fire object on the stage y = randomRange(ROOT.fire.y - ROOT.fire.height / 2, ROOT.fire.y);//this makes our particle's y initialize somewhere in the top half of our fire object on the stage ySpeed = randomRange(3, 5);//setting a random speed makes it look that much more believable, much as the random alpha value addEventListener(Event.ENTER_FRAME, update, false, 0, true);
//FireParticle.as private var bounce:Boolean = false;//start it off false if you want it to be stationary from the get go, true if you want it to start off bouncing private var xSpeed:Number = 15;//this will be our fire's speed in the x direction private var ySpeed:Number = 15;//this will be our fire's speed in the y direction
//Main.as btnBounce.addEventListener(MouseEvent.CLICK, toggleBounce, false, 0, true);//when we click on the button it will either start or stop the bounce addEventListener(Event.ENTER_FRAME, update, false, 0, true);
//Main.as private function update(e:Event) : void { if (bounce)//if bounce is true... { //...we'll make the fire move in both x and y directions fire.x += xSpeed; fire.y += ySpeed; if (fire.x - fire.width / 2 < 0)//if it hits the left side we push it back to the right { fire.x = fire.width / 2;//repositions it so it doesn't get stuck. Same for all the other ones xSpeed *= -1;//this makes the speed the opposite, ie "15" becomes "-15". Same for all the other ones } if (fire.x + fire.width / 2 > SWIDTH)//if it hits the right side we push it back to the left { fire.x = SWIDTH - fire.width / 2; xSpeed *= -1; } if (fire.y - fire.height / 2 < 0)//if it hits the top we push it back to the bottom { fire.y = fire.height / 2; ySpeed *= -1; } if (fire.y + fire.height / 2 > SHEIGHT)//if it hits the bottom we push it back to the top { fire.y = SHEIGHT - fire.height / 2; ySpeed *= -1; } } else//if it's not bouncing then it sits at it's home { fire.x = SWIDTH / 2; fire.y = SHEIGHT * 0.85; } } private function toggleBounce(me:MouseEvent) : void//when clicked... { if (!bounce)//..if it's not bouncing yet then we will make it bounce... { btnBounce.txtBounce.text = "STOP";//changes the dynamic text box bounce = true; } else//...otherwise we stop it { btnBounce.txtBounce.text = "BOUNCE";//changes the dynamic text box bounce = false; } }