topical media & game development
animation-ch15-Fireworks.ax
animation-ch15-Fireworks.ax
[swf]
[flash]
flex
package fire work(s)
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
class
[SWF(backgroundColor=0x000000)]
public class @ax-animation-ch15-Fireworks extends Sprite
{
private var balls:Array;
private var numBalls:uint = 100;
private var fl:Number = 250;
private var vpX:Number = stage.stageWidth / 2;
private var vpY:Number = stage.stageHeight / 2;
private var gravity:Number = 0.2;
private var floor:Number = 200;
private var bounce:Number = -0.6;
constructor
public function @ax-animation-ch15-Fireworks()
{
init();
}
init(s)
private function init():void
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
balls = new Array();
ball(s)
for(var i:uint = 0; i < numBalls; i++)
{
var ball:animation_ch15_Ball3D = new animation_ch15_Ball3D(3, Math.random() * 0xffffff);
balls.push(ball);
ball.ypos = -100;
ball.vx = Math.random() * 6 - 3;
ball.vy = Math.random() * 6 - 6;
ball.vz = Math.random() * 6 - 3;
addChild(ball);
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
animation / frame(s)
private function onEnterFrame(event:Event):void
{
for(var i:uint = 0; i < numBalls; i++)
{
var ball:animation_ch15_Ball3D = balls[i];
move(ball);
}
sortZ();
}
move(s)
private function move(ball:animation_ch15_Ball3D):void
{
var radius:Number = ball.radius;
ball.vy += gravity;
ball.xpos += ball.vx;
ball.ypos += ball.vy;
ball.zpos += ball.vz;
check y
if(ball.ypos > floor)
{
ball.ypos = floor;
ball.vy *= bounce;
}
check z
if(ball.zpos > -fl)
{
var scale:Number = fl / (fl + ball.zpos);
ball.scaleX = ball.scaleY = scale;
ball.x = vpX + ball.xpos * scale;
ball.y = vpY + ball.ypos * scale;
ball.visible = true;
}
else
{
ball.visible = false;
}
}
sort / Z
private function sortZ():void
{
balls.sortOn("zpos", Array.DESCENDING | Array.NUMERIC);
for(var i:uint = 0; i < numBalls; i++)
{
var ball:animation_ch15_Ball3D = balls[i];
setChildIndex(ball, i);
}
}
}
}
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.