Posted by Matt Wakeling
in Wednesday, 23 November 2011
Flash Particle Sparkler
Simple Particle Sparkler, the version above has the addition of a sparkler image to complete the bonfire night effect.
The Particle Sparkler can be seen on my sister site Flash Experiences it can be found here Particle Sparkler.
CALLING CODE
import com.Particle;
Mouse.hide();
this.objParticle = new Particle();
addChild(this.objParticle);
PARTICLE CLASS
package com
{
// Import External Classes
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.filters.BlurFilter;
import flash.geom.Point;
import flash.utils.Timer;
/**
* Name : Particle
* Coded By : Matt Wakeling
* Date : 22nd November 2011
* Description : Particle Class for Particle Effect.
*
* @author Matt Wakeling
*/
public class Particle extends Sprite
{
// Initialise Class Properties
private var objParticleTimer:Timer;
private const MAX_PARTICLES:int = 150;
//Parictle Constuctor
public function Particle()
{
super();
InitialiseParticle();
}
// InitialiseMain Method
private function InitialiseParticle():void
{
if (stage)
this.InitialiseStage();
else
addEventListener(Event.ADDED_TO_STAGE, this.InitialiseStage);
}
// InitialiseStage Method
private function InitialiseStage(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, this.InitialiseStage);
this.objParticleTimer = new Timer(1000 / 30);
this.objParticleTimer.addEventListener(TimerEvent.TIMER, this.ParticleEvent, false, 0, true);
this.objParticleTimer.start();
}
// ParticleEvent Method
public function ParticleEvent(PEEvent:TimerEvent):void
{
if (this.numChildren < MAX_PARTICLES)
{
var objPixel:Pixel = new Pixel();
objPixel.addPixel(new Point(this.mouseX, this.mouseY), new Point(((Math.random() * 10) - (Math.random() * 10)), (Math.random() * -5)));
addChild(objPixel);
objPixel = undefined;
}
}
}
}
PIXELCLASS
package com
{
import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.Event;
import flash.filters.BlurFilter;
import flash.filters.GlowFilter;
import flash.geom.Point;
/**
* Name : Pixel
* Coded By : Matt Wakeling
* Date : 23rd November 2011
* Description : Pixel Class for the Particle Class.
*
* @author Matt Wakeling
*/
public class Pixel extends Sprite
{
// Intialise Class Properties
private var pntPixelVector:Point = new Point(0,0);
private var pntTempVector:Point = new Point(0,0);
private const MINIMUM_SCALE_PERCENT:int = 10;
private const MAXIMUM_SCALE_PERCENT:int = 100;
private const PIXELSIZE:int = 3;
private const GRAVITY:Number = 1.5;
private const FRICTION:Number = 0.85;
// Pixel Constructor
public function Pixel()
{
// Constructor Code
super();
this.InitialisePixel();
}
// Initialise Method
private function InitialisePixel():void
{
// Initiailise Stage
if (stage)
this.InitialiseStage();
else
addEventListener(Event.ADDED_TO_STAGE, InitialiseStage);
}
// init Method
private function InitialiseStage(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, InitialiseStage);
addEventListener(Event.ENTER_FRAME, FrameEvent);
}
// addPixel Method
public function addPixel(pntPosition:Point, pntVector:Point):void
{
// Draw Pixel
this.graphics.clear();
this.graphics.beginFill(0xFFFFFF);
this.graphics.drawCircle(0, 0, this.PIXELSIZE);
this.x = pntPosition.x;
this.y = pntPosition.y;
this.pntPixelVector = pntVector;
this.pntTempVector = new Point(pntVector.x, pntVector.y);
addEventListener(Event.ENTER_FRAME, FrameEvent);
}
// FrameEvent Method
private function FrameEvent(FEEvent:Event):void
{
this.x += this.pntPixelVector.x;
this.y += this.pntPixelVector.y;
this.pntPixelVector.x *= this.FRICTION;
this.pntPixelVector.y += this.GRAVITY;
var numRandomScale:Number = ((Math.floor(Math.random() * (this.MAXIMUM_SCALE_PERCENT - this.MINIMUM_SCALE_PERCENT + 1)) + this.MINIMUM_SCALE_PERCENT)/100);
this.scaleX = this.scaleY = numRandomScale;
numRandomScale = undefined;
this.filters = [new GlowFilter(0xFFD700, 1, 5, 5, 2, 1), new BlurFilter((this.pntPixelVector.x * 0.5), (this.pntPixelVector.y * 0.5), 1)];
if (this.y > stage.stageHeight)
{
this.x = this.parent.mouseX; // MouseX point values are affected by Scale changes, so using parent values.
this.y = this.parent.mouseY; // MouseY point values are affected by Scale changes, so using parent values.
this.pntPixelVector.x = this.pntTempVector.x;
this.pntPixelVector.y = this.pntTempVector.y;
}
}
}
}
Matt Wakeling (
http://programmingexperiences.blogspot.com/)