flash on 2011-12-8
/**
* Copyright keenblaze ( http://wonderfl.net/user/keenblaze )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/dFuM
*/
package {
import com.actionsnippet.qbox.QuickBox2D;
import com.actionsnippet.qbox.QuickObject;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
[SWF(width='465', height='465', backgroundColor='#999999', frameRate='40')]
public class Main extends MovieClip {
public static const RATIO:int = 30;
private var _sim:QuickBox2D;
public function Main():void {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
initWorld();
var t:Timer = new Timer(1000, 5);
t.addEventListener(TimerEvent.TIMER, addBox);
t.start();
}
private function initWorld():void {
_sim = new QuickBox2D(this);
_sim.createStageWalls();
_sim.mouseDrag();
_sim.start();
}
private function addBox(event:TimerEvent):void {
var boxGraphic:Sprite = randomBox();
var w:Number = randRange(100, 20) / RATIO;
var h:Number = randRange(100, 20) / RATIO;
var box:QuickObject = _sim.addBox( { width:w, height:h, skin:Skin } );
box.userData.addChild(boxGraphic);
}
private function randomBox():Sprite {
var c:uint = Math.random() * 0xFFFFFF;
var s:Sprite = new Sprite();
s.graphics.beginFill(c);
s.graphics.drawRect( -50, -50, 100, 100);
s.graphics.endFill();
return s;
}
private function randRange(max:Number, min:Number = 0, decimals:int = 0):Number {
if (min > max) return NaN;
var rand:Number = Math.random() * (max-min + Math.pow(10, -decimals)) + min;
return int(rand * Math.pow(10, decimals)) / Math.pow(10, decimals);
}
}
}
import flash.display.Sprite;
class Skin extends Sprite{
public function Skin() {
graphics.beginFill(0x000000);
graphics.drawRect( -50, -50, 100, 100);
graphics.endFill();
}
}