Ett litet gridsystem

28/5 2009
gjort as3 flash grid rgb

gridJag byggde ett system för att rita små rutor. Så här ser det ut:

Och så här stavas det (cc):

// © 2009 lhli.net.
// Licence: http://creativecommons.org/licenses/by-sa/2.5/
<code lang="actionscript3">

package {

<code lang="actionscript3">

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;

<code lang="actionscript3">

public class Grid extends Sprite {

<code lang="actionscript3">

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]
};

<code lang="actionscript3">

private var _numberOfBoxes:int = 800;

<code lang="actionscript3">

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);

<code lang="actionscript3">

public function Grid()
{
super();
renderGrid();

<code lang="actionscript3">

buttonMode = true;
addEventListener(MouseEvent.CLICK, redrawGrid);
_timer.addEventListener(TimerEvent.TIMER, redrawGrid);
//_timer.start();

<code lang="actionscript3">

}

<code lang="actionscript3">

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;
}

<code lang="actionscript3">

if (_allBoxes[i] == undefined){
_allBoxes[i] = new Box();
addChild(_allBoxes[i]);
}

<code lang="actionscript3">

_allBoxes[i].drawBox(_box[_boxCnt], _cursor);

<code lang="actionscript3">

_boxCnt++;
}
};

<code lang="actionscript3">

private function redrawGrid(event:Event = null):void
{
_cursor = new Point();
_boxCnt = 0;

<code lang="actionscript3">

renderGrid();
}

<code lang="actionscript3">

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])
];
}
};

<code lang="actionscript3">

public static function random(n:int):int
{
return Math.floor(Math.random()*n);
}

<code lang="actionscript3">

}

<code lang="actionscript3">

}

<code lang="actionscript3">

import flash.display.Sprite;
import flash.geom.Rectangle;
import flash.geom.Point;

<code lang="actionscript3">

class Box extends Sprite {

<code lang="actionscript3">

public function Box()
{
super();
}

<code lang="actionscript3">

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();

<code lang="actionscript3">

x = cursor.x + rect.x;
y = cursor.y + rect.y;
};

 
}