Last active
April 1, 2016 06:45
-
-
Save dearenot/ac84d266cbbeb981cd56da27e980a4b4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package; | |
| import openfl.Assets; | |
| import openfl.display.Sprite; | |
| import openfl.Lib; | |
| import openfl.text.TextField; | |
| import openfl.text.TextFieldAutoSize; | |
| import openfl.text.TextFormat; | |
| class TestObject { | |
| public var testField:Int; | |
| public function new(testField:Int) { | |
| this.testField = testField; | |
| } | |
| } | |
| class Main extends Sprite | |
| { | |
| var fieldModel:Array<Array<Int>>; | |
| var WIDTH:Int = 10; | |
| var HEIGHT:Int = 10; | |
| var displayedCells:Array<Sprite>; | |
| public function new() | |
| { | |
| super(); | |
| fieldModel = []; | |
| for (i in 0...WIDTH) { | |
| var newArr:Array<Int> = []; | |
| fieldModel.push(newArr); | |
| for (j in 0...HEIGHT) { | |
| newArr.push(1); | |
| } | |
| } | |
| displayedCells = []; | |
| renderModel(fieldModel); | |
| var ar:Array<Int> = [9, 4, 1, 5, 7, 6, 9, 10, 45, 89]; | |
| trace(ar); | |
| ar.sort(function(a:Int,b:Int):Int { | |
| if (a == b) | |
| return 0; | |
| if (a > b) | |
| return 1; | |
| else | |
| return -1; | |
| }); | |
| trace(ar); | |
| var ar:Array<TestObject> = [for (i in 0...10) new TestObject(Std.int(Math.random() * 100))]; | |
| var tr1:String = " before "; | |
| for (i in 0...ar.length) { | |
| tr1 += " " + ar[i].testField + " "; | |
| } | |
| trace(tr1); | |
| ar.sort(function(a:TestObject,b:TestObject):Int { | |
| if (a.testField == b.testField) | |
| return 0; | |
| if (a.testField > b.testField) | |
| return 1; | |
| else | |
| return -1; | |
| }); | |
| var tr2:String = " after "; | |
| for (i in 0...ar.length) { | |
| tr2 += " " + ar[i].testField + " "; | |
| } | |
| trace(tr2); | |
| } | |
| function renderModel(model:Array<Array<Int>>) { | |
| clearScreen(); | |
| for (i in 0...model.length) { | |
| for (j in 0...model[i].length) { | |
| var cell:Cell = new Cell(); | |
| displayedCells.push(cell); | |
| cell.x = i * (cell.width + 5) + 20; | |
| cell.y = j * (cell.height + 5) + 20; | |
| cell.tf.text = Std.string(model[i][j]); | |
| addChild(cell); | |
| } | |
| } | |
| } | |
| function clearScreen() { | |
| for (i in 0...displayedCells.length) { | |
| displayedCells[i] = null; | |
| displayedCells.slice(0, displayedCells.length); | |
| } | |
| while (numChildren > 0) removeChildAt(0); | |
| } | |
| } | |
| class Cell extends Sprite { | |
| public var tf:TextField; | |
| public function new() { | |
| super(); | |
| graphics.beginFill(0xFF99F2); | |
| graphics.drawRect(0, 0, 20, 20); | |
| graphics.endFill(); | |
| tf = new TextField(); | |
| tf.autoSize = TextFieldAutoSize.CENTER; | |
| tf.defaultTextFormat = new TextFormat("Verdana", 20, 0x000000); | |
| tf.text = "0"; | |
| tf.x = 0; | |
| tf.y -= 5; | |
| addChild(tf); | |
| } | |
| } | |
| enum CellModelType { | |
| Passable; | |
| Impassable; | |
| } | |
| class CellModel { | |
| public var type:CellModelType; | |
| public function new(type:CellModelType) { | |
| this.type = type; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment