Skip to content

Instantly share code, notes, and snippets.

@restorer
Last active September 7, 2016 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save restorer/b7d5479d129b2bbce9d037afdc14fd52 to your computer and use it in GitHub Desktop.
Save restorer/b7d5479d129b2bbce9d037afdc14fd52 to your computer and use it in GitHub Desktop.
OpenFL 4 BunnyMark with Sprites
package ;
import openfl.Assets;
import openfl.display.Bitmap;
import openfl.display.BitmapData;
import openfl.display.FPS;
import openfl.display.Sprite;
import openfl.events.Event;
import openfl.events.MouseEvent;
class Bunny extends Bitmap {
public var speedX : Float;
public var speedY : Float;
public function new (bitmapData : BitmapData) {
super(bitmapData);
}
}
class Main extends Sprite {
private var addingBunnies : Bool;
private var bunnies : Array<Bunny>;
private var fps : FPS;
private var gravity : Float;
private var minX : Int;
private var minY : Int;
private var maxX : Int;
private var maxY : Int;
private var bitmapData : BitmapData;
public function new() {
super();
minX = 0;
maxX = stage.stageWidth;
minY = 0;
maxY = stage.stageHeight;
gravity = 0.5;
bitmapData = Assets.getBitmapData("assets/wabbit_alpha.png");
fps = new FPS();
addChild(fps);
stage.addEventListener(MouseEvent.MOUSE_DOWN, stage_onMouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp);
stage.addEventListener(Event.ENTER_FRAME, stage_onEnterFrame);
bunnies = new Array();
for (i in 0 ... 1000) {
addBunny();
}
}
private function addBunny() : Void {
var bunny = new Bunny(bitmapData);
bunny.x = 0;
bunny.y = 0;
bunny.speedX = Math.random() * 5;
bunny.speedY = (Math.random() * 5) - 2.5;
bunnies.push(bunny);
addChild(bunny);
}
private function stage_onEnterFrame(event : Event) : Void {
for (bunny in bunnies) {
bunny.x += bunny.speedX;
bunny.y += bunny.speedY;
bunny.speedY += gravity;
if (bunny.x > maxX) {
bunny.speedX *= -1;
bunny.x = maxX;
} else if (bunny.x < minX) {
bunny.speedX *= -1;
bunny.x = minX;
}
if (bunny.y > maxY) {
bunny.speedY *= -0.8;
bunny.y = maxY;
if (Math.random() > 0.5) {
bunny.speedY -= 3 + Math.random() * 4;
}
} else if (bunny.y < minY) {
bunny.speedY = 0;
bunny.y = minY;
}
}
if (addingBunnies) {
for (i in 0 ... 100) {
addBunny();
}
}
}
private function stage_onMouseDown(event : MouseEvent) : Void {
addingBunnies = true;
}
private function stage_onMouseUp(event : MouseEvent) : Void {
addingBunnies = false;
trace(bunnies.length + " bunnies");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment