Section 1
//Main (Main)
package {
import flash.events.*;
import flash.display.*;
public class Main extends Sprite {
private var t:tower;
public function Main():void{
if (stage){
init();
} else {
addEventListener(Event.ADDED_TO_STAGE, init);
};
}
private function init(_arg1:Event=null):void{
removeEventListener(Event.ADDED_TO_STAGE, init);
t = new tower();
addChild(t);
}
}
}//package
Section 2
//tblock (tblock)
package {
import flash.display.*;
public class tblock extends Sprite {
private const randomRotation:Number = 1.5;
private var w:Number;
private var h:Number;
public var _lightState:Boolean;// = false
public function tblock(_arg1:Number=0, _arg2:Number=0){
rotation = ((Math.random() * (randomRotation * 2)) - randomRotation);
w = _arg1;
h = _arg2;
lightOff();
}
public function lightOn():void{
_lightState = true;
graphics.clear();
graphics.lineStyle(1.5, 0, 1, true);
graphics.beginFill(16720418, 1);
graphics.moveTo(0, 0);
graphics.lineTo(w, 0);
graphics.lineTo(w, h);
graphics.lineTo(0, h);
graphics.lineTo(0, 0);
graphics.endFill();
}
public function lightOff():void{
_lightState = false;
graphics.clear();
graphics.lineStyle(1.5, 0, 1, true);
graphics.beginFill(0x777777, 1);
graphics.moveTo(0, 0);
graphics.lineTo(w, 0);
graphics.lineTo(w, h);
graphics.lineTo(0, h);
graphics.lineTo(0, 0);
graphics.endFill();
}
public function get lightState():Boolean{
return (_lightState);
}
}
}//package
Section 3
//tower (tower)
package {
import flash.events.*;
import flash.display.*;
import flash.text.*;
import flash.utils.*;
public class tower extends Sprite {
private const blocks_height:uint = 24;
private const max_blocks:Array;
private const txt_pregame:String = "Click to initialise";
private const blocks_y_space:uint = 3;
private const txt_win:String = "Congratulations! Click to restart";
private const blocks_x_init:uint = 8;
private const blocks_width:uint = 24;
private const blocks_x_space:uint = 3;
private const txt_game:String = "Click to stop the blocks, if all they fall, you lose.
(For extra difficulty, there is a 30ms delay when you press)";
private const txt_lost:String = "Oh no! Click to restart";
private const blocks_max_x:uint = 7;
private const blocks_max_y:uint = 13;
private const blocks_y_init:uint = 94;
private const difficulty:Array;
private var goingRight:Boolean;// = true
private var curXPos:int;// = 0
private var curYPos:int;// = 0
private var blocks:Array;// = null
private var numBlocks:uint;// = 0
private var infoBox:TextField;// = null
private var gameState:uint;// = 0
public function tower():void{
difficulty = [60, 62, 71, 82, 94, 105, 116, 128, 139, 150, 162, 173, 185];
max_blocks = [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3];
super();
if (stage){
init();
} else {
addEventListener(Event.ADDED_TO_STAGE, pre_init);
};
}
private function lightAllOff():void{
var _local2:uint;
var _local1:uint;
while (_local1 < blocks_max_x) {
_local2 = 0;
while (_local2 < blocks_max_y) {
blocks[_local1][_local2].lightOff();
_local2++;
};
_local1++;
};
}
private function init():void{
var _local2:uint;
var _local3:Number;
var _local4:Number;
var _local5:tblock;
infoBox = new TextField();
infoBox.y = (blocks_x_init + 8);
infoBox.x = (blocks_x_init + 8);
infoBox.width = (blocks_x_init + ((blocks_width + blocks_x_space) * (blocks_max_x - 1)));
infoBox.height = (blocks_y_init - (blocks_x_init + 8));
infoBox.selectable = false;
infoBox.multiline = true;
infoBox.wordWrap = true;
infoBox.text = txt_pregame;
addChild(infoBox);
blocks = new Array();
var _local1:uint;
while (_local1 < blocks_max_x) {
blocks[_local1] = [];
_local2 = 0;
while (_local2 < blocks_max_y) {
_local3 = (blocks_x_init + ((blocks_width + blocks_x_space) * _local1));
_local4 = (blocks_y_init + ((blocks_height + blocks_y_space) * _local2));
_local5 = new tblock(blocks_width, blocks_height);
_local5.x = _local3;
_local5.y = _local4;
addChild(_local5);
blocks[_local1][_local2] = _local5;
_local2++;
};
_local1++;
};
mouseEnabled = true;
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
onTick();
}
private function pre_init(_arg1:Event=null):void{
removeEventListener(Event.ADDED_TO_STAGE, pre_init);
init();
}
private function onTick():void{
var _local1:uint;
var _local2:uint;
var _local3:uint;
var _local4:int;
var _local5:int;
if (gameState == 1){
curXPos = (curXPos + (goingRight) ? 1 : (0 - 1));
if (curXPos >= (blocks_max_x - 1)){
goingRight = false;
} else {
if ((curXPos + numBlocks) <= 1){
goingRight = true;
};
};
_local1 = 0;
while (_local1 < blocks_max_x) {
blocks[_local1][curYPos].lightOff();
_local1++;
};
_local2 = 0;
while (_local2 < numBlocks) {
_local3 = (_local2 + curXPos);
if (_local3 >= blocks_max_x){
} else {
if (_local3 < 0){
} else {
blocks[_local3][curYPos].lightOn();
};
};
_local2++;
};
quickTimeout(onTick, difficulty[curYPos]);
} else {
if (gameState == 0){
_local4 = 0;
while (_local4 < blocks_max_x) {
_local5 = (blocks_max_y - 1);
while (_local5 >= 0) {
if (blocks[_local4][_local5].lightState){
if (((((_local5 + 1) < blocks_max_y)) && ((blocks[_local4][(_local5 + 1)].lightState == false)))){
blocks[_local4][(_local5 + 1)].lightOn();
};
blocks[_local4][_local5].lightOff();
};
_local5--;
};
_local4++;
};
quickTimeout(onTick, (difficulty[(blocks_max_y - 1)] / 2));
};
};
}
private function onMouseDown(_arg1:Event):void{
switch (gameState){
case 0:
initGame();
break;
case 1:
quickTimeout(stopBlock, 30);
break;
};
}
private function stopBlock():void{
var _local3:uint;
var _local4:uint;
var _local5:uint;
if (gameState != 1){
return;
};
var _local1:uint;
var _local2:uint;
while (_local2 < numBlocks) {
_local3 = (_local2 + curXPos);
if ((((((_local3 >= blocks_max_x)) || ((_local3 < 0)))) || (((!((curYPos >= (blocks_max_y - 1)))) && ((blocks[_local3][(curYPos + 1)].lightState == false)))))){
if (!(((_local3 >= blocks_max_x)) || ((_local3 < 0)))){
blocks[_local3][curYPos].lightOff();
};
_local1++;
};
_local2++;
};
if (_local1 == numBlocks){
infoBox.text = txt_lost;
gameState = 0;
_local4 = 0;
while (_local4 < numBlocks) {
_local5 = (_local4 + curXPos);
blocks[_local5][curYPos].lightOn();
_local4++;
};
return;
};
numBlocks = (numBlocks - _local1);
curYPos = (curYPos - 1);
if (numBlocks > max_blocks[curYPos]){
numBlocks = max_blocks[curYPos];
};
curXPos = Math.floor((Math.random() * blocks_max_x));
if (Math.random() < 0.5){
goingRight = !(goingRight);
};
if (curYPos < 0){
gameState = 0;
infoBox.text = txt_win;
};
}
private function initGame():void{
lightAllOff();
curXPos = 0;
curYPos = (blocks_max_y - 1);
gameState = 1;
numBlocks = 3;
goingRight = true;
infoBox.text = txt_game;
}
private function quickTimeout(_arg1:Function, _arg2:uint):void{
var to:Timer;
var tmpFunc:Function;
var func = _arg1;
var delay = _arg2;
to = new Timer(delay, 1);
tmpFunc = function (_arg1:TimerEvent):void{
to.removeEventListener(TimerEvent.TIMER_COMPLETE, tmpFunc);
func();
};
to.addEventListener(TimerEvent.TIMER_COMPLETE, tmpFunc);
to.start();
}
}
}//package