Grenade Explosion
@author: Dennis Wilson
@see: http://www.abakia.de/
/**
* Copyright bytecode_hero ( http://wonderfl.net/user/bytecode_hero )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/qn5t
*/
package {
//@author: Dennis Wilson
//@see: http://www.abakia.de/
import flash.display.*;
import flash.events.*;
import flash.filters.*;
import flash.geom.*;
import flash.text.TextField;
[SWF(frameRate="75", backgroundColor="#000000")]
public class Explosion extends Sprite {
public const MAX_W : uint = 500;
public const MAX_H : uint = 500;
public const HALF_MAX_W : uint = MAX_W / 2;
public const HALF_MAX_H : uint = MAX_H / 2;
public const MAX_PARTICLES : uint = 100;
public const RADIUS : uint = 10;
public const EXPLODE_SPEED : uint = 5;
public const PI_DIV_180 : Number = Math.PI / 180;
public const GRAVITY : Number = 0.5;
public var frame : uint = 0;
public var particles : Array = [];
public var container : Sprite;
public var bmpdSmoke : BitmapData;
public var tf : TextField;
public var bitmap : Sprite;
public var bmpdGlow : BitmapData;
public var smokeBlur : BitmapFilter;
public var fragmentBlur : BitmapFilter;
public var x_shake : Number;
public var shiver_till: uint = 0;
public function Explosion() {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.quality = StageQuality.LOW;
init();
addEventListener(Event.ENTER_FRAME, update);
}
/* Helper */
private function init() : void {
smokeBlur = new BlurFilter(2, 10);
fragmentBlur = new BlurFilter(15, 10)
bmpdSmoke = new BitmapData(MAX_W, MAX_H, true, 0x00ffffff);
bmpdGlow = new BitmapData(MAX_W, MAX_H, true, 0x00ffffff);
container = new Sprite();
container.x = 0;
container.y = 0;
bitmap = new Sprite();
bitmap.addChild(new Bitmap(bmpdSmoke));
bitmap.addChild(new Bitmap(bmpdGlow));
bitmap.mouseEnabled = true;
bitmap.buttonMode = true;
bitmap.useHandCursor = true;
bitmap.alpha = 1;
bitmap.addEventListener(MouseEvent.CLICK, onClick);
addChild(bitmap);
tf = new TextField();
tf.text = 'click-click-boom!';
tf.x = 10;
tf.y = 10;
tf.height = 15;
tf.blendMode = BlendMode.INVERT;
addChild(tf);
}
private function spawnFragments(x:uint, y:uint) : void {
var p : Particle;
for(var i : int = 0;i < MAX_PARTICLES; i++) {
p = new Particle();
p.x = p.x_orig = x;
p.y = p.y_orig = y;
p.x_to = x + Math.sin((Math.random() * 1) * PI_DIV_180) * (Math.random() * RADIUS);
p.y_to = y + Math.cos((Math.random() * 2) * PI_DIV_180) * (Math.random() * RADIUS);
// random power to specified direction.
// creepy math. wanna fork? ;-)
var a:Number = y - p.y_to;
var c:Number = x - p.x_to;
p.x_speed = Math.sin((a/c)) * Math.random()*10;
p.y_speed = Math.cos((a/c)) * Math.random()*10;
p.perish_at = Math.round(Math.random()*100+20);
p.alpha_to = 0.15;
container.addChild(p);
particles[particles.length] = p;
}
shiver_till = frame + 20;
x_shake = 4;
}
/* Events */
private function onClick(event : MouseEvent) : void {
spawnFragments(mouseX, mouseY);
}
private function update(e : Event = null) : void {
frame++;
//move fragments
var j : Number = 0;
for each(var p:Particle in particles) {
p.x += p.x_speed;
p.y += p.y_speed;
p.y_speed += GRAVITY;
p.alpha = p.alpha + ( p.alpha_to - p.alpha) / 2;
if(p.perish_at < (p.y - p.y_orig)) {
p.alpha_to = 0;
}
if(p.y > MAX_H) {
particles.splice(particles.indexOf(p),1);
container.removeChild(p);
}
}
// shiver
if(frame <= shiver_till) {
bitmap.x = Math.random()*x_shake*Math.sin(frame);
bitmap.y = Math.random()*2*Math.sin(frame);
x_shake = x_shake + ( 0 - x_shake)/10;
} else {
bitmap.x = 0;
bitmap.y = 0;
}
// draw smoke
bmpdSmoke.draw(container, null, null, BlendMode.ADD);
bmpdSmoke.draw(container, null, null, BlendMode.ADD);
bmpdSmoke.colorTransform(bmpdSmoke.rect, new ColorTransform(1, 1, 1, 1, 0xff, 0xff, 0xff, 0));
bmpdSmoke.scroll(0, -2);
bmpdSmoke.applyFilter(bmpdSmoke, bmpdSmoke.rect, new Point(), smokeBlur);
// draw fragments
bmpdGlow.draw(container, null, null, BlendMode.ADD);
bmpdGlow.applyFilter(bmpdGlow, bmpdGlow.rect, new Point(), fragmentBlur);
bmpdGlow.draw(container,null,null, BlendMode.ADD);
bmpdGlow.draw(container,null,null, BlendMode.ADD);
}
}
}
import flash.display.Sprite;
class Particle extends Sprite {
public var x_to :Number = 0;
public var y_to :Number = 0;
public var x_orig : Number =0;
public var y_orig : Number =0;
public var x_speed : Number = 0;
public var y_speed : Number = 0;
public var alpha_to : Number = 1;
public var color : uint = 0;
public var perish_at:uint=0;
public function Particle() {
const COLORS:Array = [0xf26700, 0xf29400, 0xff0000];
color = COLORS[Math.round(Math.random()*(COLORS.length-1))];
graphics.beginFill(color);
graphics.drawCircle(-2, -2, 2);
graphics.endFill();
}
}