Ett litet gridsystem
28/5 2009gjort ▦ as3 flash grid rgb
Jag byggde ett system för att rita små rutor. Så här ser det ut:
[swfobj src=”https://blog.lhli.net/wp-content/uploads/2009/05/box.swf” width=”430″ height=”430″ align=”left”]
Och så här stavas det (cc):
// Licence: http://creativecommons.org/licenses/by-sa/2.5/
package {
import flash.display.Sprite;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class Grid extends Sprite {
private static const TOTAL_WIDTH:int = 430;
private static const COL_WIDTH:int = 10;
private static const GAP:int = 2;
private static const BOX_SIZES:Object = {
small: [COL_WIDTH, COL_WIDTH],
medium: [COL_WIDTH*2+GAP, COL_WIDTH],
large: [COL_WIDTH*2+GAP, COL_WIDTH*2+GAP]
};
private var _numberOfBoxes:int = 800;
private var _cursor = new Point(-(COL_WIDTH*2 + GAP*2),0);
private var _allBoxes:Array = [];
private var _box:Array = [];
private var _boxCnt:int = 0;
private var _timer:Timer = new Timer(10);
public function Grid()
{
super();
renderGrid();
buttonMode = true;
addEventListener(MouseEvent.CLICK, redrawGrid);
_timer.addEventListener(TimerEvent.TIMER, redrawGrid);
//_timer.start();
}
private function renderGrid():void
{
for (var i:int = 0; i < _numberOfBoxes; i++) { //check if box is complete, calculate new box and move cursor if (_boxCnt >= _box.length){
_boxCnt = 0;
calculateBox();
_cursor.x += COL_WIDTH*2 + GAP*2;
}
// check if row is complete, move cursor
if (_cursor.x + COL_WIDTH*2 >= TOTAL_WIDTH){
_cursor.x = 0;
_cursor.y += COL_WIDTH*2 + GAP*2;
}
if (_allBoxes[i] == undefined){
_allBoxes[i] = new Box();
addChild(_allBoxes[i]);
}
_allBoxes[i].drawBox(_box[_boxCnt], _cursor);
_boxCnt++;
}
};
private function redrawGrid(event:Event = null):void
{
_cursor = new Point();
_boxCnt = 0;
renderGrid();
}
private function calculateBox():void
{
_box = [];
var item:int;
item = random(3);
// go through all possible cases for area the size of the largest box
if (item == 2) _box = [
new Rectangle(0,0,BOX_SIZES.large[0],BOX_SIZES.large[1])
];
else if (item == 1) {
item = random(2);
if (item == 1) _box = [
new Rectangle(0,0,BOX_SIZES.medium[0],BOX_SIZES.medium[1]),
new Rectangle(0,COL_WIDTH+GAP,BOX_SIZES.medium[0],BOX_SIZES.medium[1])
];
else _box = [
new Rectangle(0,0,BOX_SIZES.medium[0],BOX_SIZES.medium[1]),
new Rectangle(0,COL_WIDTH+GAP,BOX_SIZES.small[0],BOX_SIZES.small[1]),
new Rectangle(COL_WIDTH+GAP,COL_WIDTH+GAP,BOX_SIZES.small[0],BOX_SIZES.small[1])
];
}
else {
item = random(2);
if (item == 1) _box = [
new Rectangle(0,0,BOX_SIZES.small[0],BOX_SIZES.small[1]),
new Rectangle(COL_WIDTH+GAP,0,BOX_SIZES.small[0],BOX_SIZES.small[1]),
new Rectangle(0,COL_WIDTH+GAP,BOX_SIZES.medium[0],BOX_SIZES.medium[1])
];
else _box = [
new Rectangle(0,0,BOX_SIZES.small[0],BOX_SIZES.small[1]),
new Rectangle(COL_WIDTH+GAP,0,BOX_SIZES.small[0],BOX_SIZES.small[1]),
new Rectangle(0,COL_WIDTH+GAP,BOX_SIZES.small[0],BOX_SIZES.small[1]),
new Rectangle(COL_WIDTH+GAP,COL_WIDTH+GAP,BOX_SIZES.small[0],BOX_SIZES.small[1])
];
}
};
public static function random(n:int):int
{
return Math.floor(Math.random()*n);
}
}
}
import flash.display.Sprite;
import flash.geom.Rectangle;
import flash.geom.Point;
class Box extends Sprite {
public function Box()
{
super();
}
public function drawBox(rect:Rectangle, cursor:Point):void
{
graphics.clear();
var c:int = Grid.random(256);
graphics.beginFill(Grid.random(Math.pow(256,3)));
graphics.lineStyle(1,0,0);
graphics.drawRect(0, 0, rect.width, rect.height);
graphics.endFill();
x = cursor.x + rect.x;
y = cursor.y + rect.y;
};