forked from: MandMFrenesi
// forked from jbraccini's MandMFrenesi
package {
import flash.display.GradientType;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.geom.Matrix;
import flash.text.AntiAliasType;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.Timer;
import flash.utils.getTimer;
import net.hires.debug.Stats;
[SWF(backgroundColor="#ffffff", frameRate=25)]
public class WaterBalloons extends Sprite {
private var _floor:Sprite;
private var _background:Sprite;
private var _balloonsHolder:Sprite;
private var _shadowsHolder:Sprite;
private var _stageObjects:Array;
private var _sequencer:Timer;
private var _render:Timer;
[Embed(systemFont='serif',
fontName='font',
mimeType='application/x-font',
fontWeight='bold',
advancedAntiAliasing='true'
)]
private var font:Class;
private const MAX_BALLOONS:Number = 50;
private const INC_GRAVITY:Number = 1;
public function WaterBalloons(){
init();
}
private function init():void {
_stageObjects = new Array();
_sequencer = new Timer(50);
_sequencer.addEventListener(TimerEvent.TIMER, handleSequencer);
_render = new Timer(33);
_render.addEventListener(TimerEvent.TIMER, handleRender);
addChild(new Stats());
setStage();
/* var balloon:Object = createWaterBalloon(0xFF0000, 20);
balloon.shape.x = stage.stageWidth / 2;
balloon.shape.y = stage.stageHeight / 2;
balloon.shape.getChildByName("base").rotation = 45;
addChild(balloon.shape); */
start();
}
private function handleRender(e:TimerEvent):void {
_floor.y--;
if(_floor.y < -_floor.height) _floor.y = 465;
var balloon:Object;
for(var i:Number = 0; i < _stageObjects.length; i++){
balloon = _stageObjects[i];
balloon.shape.scaleX = .9 + (Math.sin(balloon.seed) * .1);
balloon.shape.scaleY = .9 + (Math.cos(balloon.seed) * .1);
balloon.shape.y += balloon.yvel;
balloon.shape.x += balloon.xvel;
balloon.shape.getChildByName("base").rotation += balloon.rvel;
balloon.seed+=.25;
balloon.shadow.x = balloon.shape.x;
balloon.shape.y < _floor.y ? balloon.yvel += INC_GRAVITY : balloon.yvel = -balloon.yvel;
if(balloon.shape.x < 0 || balloon.shape.x > stage.stageWidth){
balloon.xvel = -balloon.xvel;
balloon.rvel = -balloon.rvel;
}
if(getTimer() - balloon.born > balloon.life){
_balloonsHolder.removeChild(balloon.shape);
_shadowsHolder.removeChild(balloon.shadow);
_stageObjects.splice(i, 1);
}
}
}
private function handleSequencer(e:TimerEvent):void {
if(_stageObjects.length < MAX_BALLOONS){
var balloon:Object = createWaterBalloon(Math.random() * 0xFFFFFF, randRange(5, 15));
balloon.shape.x = stage.mouseX;
balloon.shape.y = stage.mouseY;
balloon.shadow.y = _floor.y + 30;
_balloonsHolder.addChild(balloon.shape);
_shadowsHolder.addChild(balloon.shadow);
_stageObjects.push(balloon);
}
}
private function start():void {
_sequencer.start();
_render.start();
}
private function setStage():void {
_background = new Sprite();
_floor = new Sprite();
_balloonsHolder = new Sprite();
_shadowsHolder = new Sprite();
var matrix:Matrix = new Matrix();
matrix.createGradientBox(stage.stageWidth, 200, (90*(Math.PI /180)));
// Defines background
_background.graphics.beginGradientFill(GradientType.LINEAR, [0x00bff3, 0xFFFFFF], [1.0, 1.0], [0, 255], matrix);
_background.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
addChild(_background);
// Defines floor
_floor.graphics.beginGradientFill(GradientType.LINEAR, [0x39b54a, 0x197b30], [1.0, 1.0], [0, 255], matrix);
_floor.graphics.drawRect(0, 0, stage.stageWidth, 50);
_floor.y = 465 - _floor.height;
addChild(_floor);
addChild(_shadowsHolder);
addChild(_balloonsHolder);
}
private function createWaterBalloon(color:uint, size:int):Object {
var balloonObject:Object = new Object();
var balloon:Sprite = new Sprite();
var base:Sprite = new Sprite();
var shine:Sprite = new Sprite();
var shadow:Sprite = new Sprite();
var matrix:Matrix = new Matrix();
var tf:TextField = new TextField();
var fmt:TextFormat = new TextFormat();
matrix.createGradientBox(10, 10, (90*(Math.PI /180)), 0, -10);
base.name = "base";
// Defines balloon's gradient shape
base.graphics.beginGradientFill(GradientType.RADIAL, [color, color/5], [1.0, 1.0], [0, 100]);
base.graphics.drawCircle(0, 0, size);
base.graphics.endFill();
// Defines balloon's reflex
shine.graphics.beginGradientFill(GradientType.LINEAR, [0xFFFFFF, 0xFFFFFF], [0.8, 0], [0, 255], matrix);
shine.graphics.drawCircle(0, -2, size - 3);
shine.graphics.endFill();
// Defines balloon's little reflection
// balloon.graphics.beginFill(0xFFFFFF);
// balloon.graphics.drawCircle(size / 3, - size /3, size / 8);
// balloon.graphics.endFill();
/* balloon.graphics.beginFill(color);
balloon.graphics.moveTo(0, -size);
balloon.graphics.lineTo(-3, -size-3);
balloon.graphics.lineTo(3, -size-3);
balloon.graphics.lineTo(0, -size);
balloon.graphics.endFill();
*/
fmt.font = "font";
fmt.size = size+5;
fmt.color = 0xFFFFFF;
tf.defaultTextFormat = fmt;
tf.text = "m";
tf.autoSize = "left";
tf.embedFonts = true;
tf.selectable = false;
tf.x = -(tf.width/2);
tf.y = -((tf.height/2)+3);
tf.antiAliasType = AntiAliasType.NORMAL;
base.addChild(tf);
balloon.addChild(base);
balloon.addChild(shine);
// Defines balloon's shadow
var shadowWidth:Number = size * 2;
var shadowHeight:Number = 5;
matrix.createGradientBox(shadowWidth, shadowHeight, 0, -(shadowWidth/2), -(shadowHeight/2));
shadow.graphics.beginGradientFill(GradientType.RADIAL, [0x000000, 0x000000], [0.2, 0], [0, 255], matrix);
shadow.graphics.drawEllipse(-(shadowWidth/2), -(shadowHeight/2) , shadowWidth, shadowHeight);
shadow.graphics.endFill();
balloonObject.shape = balloon;
balloonObject.shadow = shadow;
balloonObject.yvel = 0;
balloonObject.xvel = randRange(3, 15);
balloonObject.rvel = randRange(1, 10);
balloonObject.life = randRange(2000, 8000);
balloonObject.born = getTimer();
balloonObject.seed = 0;
return balloonObject;
}
private function randRange(start:Number, end:Number) : Number {
return Math.floor(start +(Math.random() * (end - start)));
}
}
}