package org.flixel
{
/**
* This is a simple particle class that extends the default behavior
* of FlxSprite
to have slightly more specialized behavior
* common to many game scenarios. You can override and extend this class
* just like you would FlxSprite
. While FlxEmitter
* used to work with just any old sprite, it now requires a
* FlxParticle
based class.
*
* @author Adam Atomic
*/
public class FlxParticle extends FlxSprite
{
/**
* How long this particle lives before it disappears.
* NOTE: this is a maximum, not a minimum; the object
* could get recycled before its lifespan is up.
*/
public var lifespan:Number;
/**
* Determines how quickly the particles come to rest on the ground.
* Only used if the particle has gravity-like acceleration applied.
* @default 500
*/
public var friction:Number;
/**
* Instantiate a new particle. Like FlxSprite
, all meaningful creation
* happens during loadGraphic()
or makeGraphic()
or whatever.
*/
public function FlxParticle()
{
super();
lifespan = 0;
friction = 500;
}
/**
* The particle's main update logic. Basically it checks to see if it should
* be dead yet, and then has some special bounce behavior if there is some gravity on it.
*/
override public function update():void
{
//lifespan behavior
if(lifespan <= 0)
return;
lifespan -= FlxG.elapsed;
if(lifespan <= 0)
kill();
//simpler bounce/spin behavior for now
if(touching)
{
if(angularVelocity != 0)
angularVelocity = -angularVelocity;
}
if(acceleration.y > 0) //special behavior for particles with gravity
{
if(touching & FLOOR)
{
drag.x = friction;
if(!(wasTouching & FLOOR))
{
if(velocity.y < -elasticity*10)
{
if(angularVelocity != 0)
angularVelocity *= -elasticity;
}
else
{
velocity.y = 0;
angularVelocity = 0;
}
}
}
else
drag.x = 0;
}
}
/**
* Triggered whenever this object is launched by a FlxEmitter
.
* You can override this to add custom behavior like a sound or AI or something.
*/
public function onEmit():void
{
}
}
}