Repeat on yourself…

This is (believe it or not) a single 40 x 40 pixel tile. The app places a tile across the entire screen then randomly rotates it by 90, 180, 270 or 0 degrees. If a colour inverse is required to keep the pattern going then it does (I’m just using TweenLite to do that although an identical effect can be achieved using AS3 ColourTransform). Here’s the code.


package {
import atiles.Tile;

import flash.display.Sprite;
import flash.events.Event;

/**
* @author Dan
*/

[SWF(width="1440",height="900",frameRate="30",backgroundColor="#FFFFFFF")]

public class Main extends Sprite {

private static const TILE_DIMENSION : Number = 40;

public function Main() {

init();
this.stage.addEventListener(Event.RESIZE, stageResizeHandler);
}

private function stageResizeHandler(event : Event) : void
{
for (var i : int = 0; i < this.numChildren; i++) {
removeChild(this.getChildAt(i));
}

init();
}

public function init(e : Event = null) : void
{
for (var i : int = 0; i < Math.ceil(stage.stageWidth/40); i++) {

for (var j : int = 0; j < Math.ceil(stage.stageHeight/40); j++) {

createTile(i, j);

}

}
}

private function createTile(Xpos : Number, Ypos : Number) : void
{
var tile : Tile;

tile = new Tile(new ATile());
tile.x = Xpos * TILE_DIMENSION;
tile.y = Ypos * TILE_DIMENSION;
addChild(tile);

var rotationOfTile : Number =  Math.floor(Math.random()*4);
tile.rotation = rotationOfTile * 90;

var reflectionOfTile : Number = Math.floor(Math.random()*2);

var isPosRotRefEven : Boolean = (Xpos + Ypos + rotationOfTile + reflectionOfTile)/2 == Math.floor((Xpos + Ypos + rotationOfTile + reflectionOfTile)/2);

if(reflectionOfTile == 1) {
tile.reflection();
}

if(isPosRotRefEven) {
tile.colourInvert();
}

}
}
}

and you’ll need the tile class (essentially it’s just a holder for now, but we could put some animation code in here at some point). It holds the functions for colour inverse and rotation and flipping. Stick it in an ‘atiles’ package.


package atiles {
import com.greensock.TweenMax;

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;

/**
* @author Dan
*/

public class Tile extends Sprite {

private var _clip : ATile;

public function Tile(clip : ATile) {

_clip = clip;
addChild(_clip);
}

public function reflection() : void
{
_clip.scaleX = -1;
}

public function colourInvert() : void
{
TweenMax.to(_clip, 0, {colorMatrixFilter:{contrast:-1}});
}
}
}

The actual Tile class is a 40×40 pixel MovieClip created in the Flash IDE. Then publish the FLA as a SWC and link it to your project as an external library. Then you can use it as a regular class.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s