Skip to content

Instantly share code, notes, and snippets.

@smebberson
Created July 2, 2012 13:22
Show Gist options
  • Save smebberson/3033231 to your computer and use it in GitHub Desktop.
Save smebberson/3033231 to your computer and use it in GitHub Desktop.
NME (haxe) example
<?xml version="1.0" encoding="utf-8"?>
<project>
<app title="Squares" main="Squares" file="Squares"
package="com.scottmebberson.squares" version="0.0.1"
company="Scott Mebberson" ></app>
<window width="0" height="0" fps="30" orientation="landscape" resizable="false" unless="target_flash" />
<set name="BUILD_DIR" value="Export" ></set>
<classpath name="./" ></classpath>
<haxelib name="nme" ></haxelib>
<ndll name="std" />
<ndll name="regexp" />
<ndll name="zlib" />
<ndll name="nme" haxelib="nme" />
</project>
package;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.Lib;
class Squares extends Sprite {
static var rectangleWidth : Int = 50;
static var rectangleHeight : Int = 50;
// when main is executed, let's instantiate a new Squares class
public static function main () {
Lib.current.addChild (new Squares());
}
// this function will be called upon instantiation of a new Squares class
public function new () {
super();
construct();
}
// setup the basics
private function construct () : Void {
Lib.current.stage.align = StageAlign.TOP_LEFT;
Lib.current.stage.scaleMode = StageScaleMode.NO_SCALE;
Lib.current.stage.addEventListener(MouseEvent.CLICK, handleMouseClick);
drawRectangles();
}
private function drawRectangles () : Void {
// draw 200 squares
for (loopIndex in 0...200) {
drawRectangle();
}
}
private function drawRectangle () : Shape {
// draw the actual shape, that will be added to stage
var rectangle : Shape = new Shape();
rectangle.graphics.beginFill(0x54BEFF, 0.6);
rectangle.graphics.drawRect(0, 0, rectangleWidth, rectangleHeight);
rectangle.graphics.endFill();
Lib.current.addChild(rectangle);
// position the shape on stage
var x : Int = Math.floor(Math.random() * Lib.current.stage.stageWidth-rectangleWidth);
var y : Int = Math.floor(Math.random() * Lib.current.stage.stageHeight-rectangleHeight);
// make sure the number is greater than zero
rectangle.x = (x > 0) ? x : 0;
rectangle.y = (y > 0) ? y : 0;
return rectangle;
}
// on stage click, let's start the fun all over again!
private function handleMouseClick (event : MouseEvent) : Void {
while (Lib.current.numChildren > 0) {
Lib.current.removeChildAt(0);
}
drawRectangles();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment