forked from: Random Invaders
this was inspired by invader fractal:
http://www.levitated.net/daily/levInvaderFractal.html
Currently just different colors and different selection method
than noonat's version
/**
* Copyright Magnum_Opus ( http://wonderfl.net/user/Magnum_Opus )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/53nZ
*/
// forked from noonat's Random Invaders
// this was inspired by invader fractal:
// http://www.levitated.net/daily/levInvaderFractal.html
//Currently just different colors and different selection method
//than noonat's version
package {
import flash.display.*;
import flash.geom.*;
import flash.text.*;
public class Invaders extends Sprite {
private const BACKGROUND_COLOR:uint = 0xff000000;
private const INVADER_WIDTH:int = 5;
private const INVADER_HEIGHT:int = 5;
private const MARGIN:int = 2;
private const SCALE:int = 4;
private var _bitmap:Bitmap;
private var _bitmapData:BitmapData;
private var _invader:BitmapData;
private var _invaderRect:Rectangle;
private var _list:Array;
private var _size:int;
public function Invaders() {
// setup the stage
stage.scaleMode = StageScaleMode.NO_SCALE;
_bitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, true, BACKGROUND_COLOR);
_bitmap = new Bitmap(_bitmapData);
_bitmap.scaleX = _bitmap.scaleY = SCALE;
addChild(_bitmap);
// render some invaders
_list = new Array(32676);
_size = _list.length;
for(var i:int=0; i < _size ;i++){
_list[i]=i+1;
}
shuffle();
_invader = new BitmapData(INVADER_WIDTH, INVADER_HEIGHT, true, 0x00000000);
_invaderRect = new Rectangle(0, 0, INVADER_WIDTH, INVADER_HEIGHT);
var sx:int = INVADER_WIDTH + MARGIN;
var sy:int = INVADER_HEIGHT + MARGIN;
var sxn:int = Math.floor(stage.stageWidth / sx);
var syn:int = Math.floor(stage.stageHeight / sy);
for (var y:int=0; y < syn; ++y) {
for (var x:int=0; x < sxn; ++x) {
generateInvader();
copyInvader(MARGIN+x*sx, MARGIN+y*sy);
}
}
}
/** invaders **/
private var _point:Point = new Point(0, 0);
public function copyInvader(x:int, y:int):void {
_point.x = x;
_point.y = y;
_bitmapData.copyPixels(_invader, _invaderRect, _point, null, null, true);
}
public function generateInvader():void {
var color:uint = 0xff00ff00; //invader green
_invader.lock();
_invader.fillRect(_invaderRect, 0x4000ff00); //slight shading to help show tiny invaders
var span:int = Math.ceil(INVADER_WIDTH / 2);
for (var y:int=0; y < INVADER_HEIGHT; ++y) {
var r:int = random();
for (var x:int=0; x < span; ++x) {
if (r & (1 << x)) {
_invader.setPixel32(x, y, color);
_invader.setPixel32(INVADER_WIDTH - 1 - x, y, color);
}
}
}
_invader.unlock();
}
public function shuffle():void {
for(var i:int = (_size - 1); i > 0; i--){
var r:int = Math.floor(Math.random() * i);
var tmp:int;
tmp = _list[i];
_list[i]=_list[r];
_list[r] = tmp;
}
}
private var _currentInvader:int = 0;
public function random():int {
_currentInvader++;
return _list[_currentInvader - 1];
}
}
}