Pattern Experiment 1
Random pattern + movement
/**
* Copyright itsDcTurner ( http://wonderfl.net/user/itsDcTurner )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/wCWK
*/
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
import gs.OverwriteManager;
[SWF(backgroundColor="#000000", frameRate=60)]
public class Main extends Sprite
{
private const grid:Point = new Point(20, 20); // x:columns, y:rows
private const patternBank:Array = [new<int>[0, 1, 0, 1, 0, 1, 0, 1, 0], new<int>[1,0,1,0,1,0,1,0,1], new<int>[1,1,1,0,0,0,1,1,1], new<int>[0,0,0,1,1,1,0,0,0]];
private var cellWidth:Number;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
OverwriteManager.init(OverwriteManager.NONE);
draw();
}
private function draw():void
{
cellWidth = stage.stageWidth / grid.x;
var loc:Point = new Point(0, 0);
for (var i:int = 0; i < grid.x; i ++)
{
for (var j:int = 0; j < grid.y; j ++)
{
loc.x = i * cellWidth;
loc.y = j * cellWidth;
addPattern(loc);
}
}
}
private function addPattern($loc:Point):Pattern {
var p:Pattern = new Pattern(cellWidth, patternBank[int(Math.random() * patternBank.length)], 0xdddddd);
p.x = $loc.x;
p.y = $loc.y;
addChild(p);
return p;
}
}
}
import flash.display.Sprite;
import flash.geom.Point;
import gs.TweenMax;
import gs.easing.*;
class Pattern extends Sprite {
private const minTime:Number = .5;
private const maxTime:Number = 4;
public var v :Vector.<int>;
public var size :Number;
public var gridSize :int;
public var col :uint;
private var cellSize:Number;
private var halfSize:Number;
function Pattern($size:Number, $pat:Vector.<int>, $col:uint):void {
v = $pat;
trace(v);
col = $col;
size = $size;
gridSize = Math.sqrt(v.length);
drawPattern();
rotate()
}
private function rotate():void
{
var rotAmount:int = int((Math.random() * 8)-4) * 90;
var time:Number = randomBetween(minTime, maxTime);
TweenMax.to(this, time, { rotation:rotAmount, ease:Elastic.easeOut } );
TweenMax.to(this, time * .5, { alpha:Math.random() } );
TweenMax.delayedCall(Math.random()*30, rotate);
}
private function randomBetween($min:Number, $max:Number):Number {
return minTime + Math.random() * ($max - $min);
}
private function drawPattern():void
{
var count:int = 0;
var loc:Point = new Point(0, 0);
graphics.beginFill(col);
cellSize = size / gridSize;
halfSize = size * .5;
trace("cellsize: " + cellSize);
for (var i:int = 0; i < gridSize; i++)
{
for (var j:int = 0; j < gridSize; j++)
{
trace("i: " + i +", j: " + j +", = :" + v[count]);
if (v[count]) {
loc.x = (i * cellSize) + halfSize;
loc.y = (j*cellSize) + halfSize;
addSquare(loc);
}
count++;
}
}
}
private function addSquare($loc:Point):void {
graphics.beginFill(col);
graphics.drawRect($loc.x-halfSize, $loc.y-halfSize, cellSize, cellSize);
graphics.endFill();
trace("square at " + $loc.x + ", " + $loc.y);
}
}