//根据方向值,取得此位置的下一个位置 function getNextPos(d):Pos{ var nextPos:Pos; nextPos=new Pos(col,row); switch (d) { case 0 : nextPos.col++;
break; case 1 : nextPos.row++; break; case 2 : nextPos.col--; break; case 3 : nextPos.row--; break; } return nextPos; } }
//************Wall.as************ //用来构建游戏中的墙壁 class Wall extends MovieClip { var brickMatrix:Array;//表示墙壁外形的数组 var left:Number;//左偏移量 var top:Number;//上偏移量 var bricks:Array;//用来存放砖块电影剪辑的数组 var _stepSize:Number;//每块砖的偏移量
//构造函数 function Wall() { left = 0; top = 0; _stepSize = 40;
}
//设置墙壁外形数组 function setMatrix(b) {
bricks = new Array(); brickMatrix = b; }
//按照墙壁外形数组修墙 function build():Void { var index = 0; for (var i = 0; i<brickMatrix.length; i++) { for (var j = 0; j<brickMatrix[0].length; j++) { if (brickMatrix[i][j] == 1) { //数组值为1,画一块砖 bricks[index] = attachMovie("Brick", "bricks"+index, this.getNextHighestDepth()); bricks[index]._x = left+j*_stepSize; bricks[index]._y = top+i*_stepSize; index++; } } } } }
//*************Target.as***************** class Target extends MovieClip{ var pos:Pos; var finished:Boolean; var left:Number; var top:Number; var _stepSize:Number; function Target(){ finished=false; left=0; top=0; _stepSize=40;
} function setPos(p){ pos=p; this._x=left+pos.col*_stepSize; this._y=top+pos.row*_stepSize; } }
//*************Player.as************** //游戏中的player class Player extends MovieClip {
var _stepSize:Number;//步长 var _wall:MovieClip;//墙壁电影剪彩辑 var startPos:Pos;//初始置 var currentPos:Pos;//当前位置 var left:Number;//左边距 var top:Number;//上边距 var dir:Number;//移动方向 var boxes:Array; var player_path_stack:Array;//玩家移动的路线 var boxes_path_stack:Array;//箱子移动的路线
//构造函数,初始化 function Player() { _stepSize = 40; currentPos = null; left = 0; top = 0; dir = 0; player_path_stack=new Array(); boxes_path_stack=new Array();
boxes = new Array(); }
//设置玩家的起始位置 function setStartPos(p) { startPos = new Pos(p.col, p.row); currentPos = startPos; this._x = currentPos.col*_stepSize+left; this._y = currentPos.row*_stepSize+top;
}
//为玩家指定一个wall实例 function setWall(w) { _wall = w; }
//为玩家指定一个boxes数组,存有所有的箱子实例 function setBoxes(b) { boxes = b; }
//通过按键控制玩家 function setKeyHandle() { this.onKeyDown = function() {
if (Key.isDown(Key.LEFT)) { dir = 2;//左键
} else if (Key.isDown(Key.RIGHT)) { dir = 0;//右键
} else if (Key.isDown(Key.UP)) { dir = 3;//上键
} else if (Key.isDown(Key.DOWN)) { dir = 1;//下键
} var nextObject = this.getNextObject(); //取得玩家下一个位置上的物体
if (nextObject == "BOX") { //玩家下一个位置上是个箱子 var box_pushed = getPushedBox();//取得此位置上的这个箱子
//被推到的箱子存在 if (box_pushed) {
if (box_pushed.getNextObject() == "NOTHING") {//被推箱子的下个位置没有障碍
} else if (nextObject == "NOTHING") {//玩家的下个位置玩障碍 this.move(); boxes_path_stack.push(null); }
}; Key.addListener(this); //监听按键输入 }
//取得被推到的箱子 function getPushedBox() { var nextPos:Pos; nextPos =currentPos.getNextPos(dir);
for (var i = 0; i<boxes.length; i++) { if (boxes[i].pos.equals(nextPos)) { return boxes[i]; break; //trace(box_pushed); } } return null; }
//取得玩家下个位置上的物块类型( 墙壁, 箱子, 无障碍) function getNextObject():String { //var obj:String; var nextPos:Pos; nextPos = currentPos.getNextPos(dir);
if (_wall.brickMatrix[nextPos.row][nextPos.col] == 1) { return "WALL"; } for (var i = 0; i<boxes.length; i++) { if (boxes[i].pos.equals(nextPos)) { return "BOX"; } } return "NOTHING"; }
//移到下个位置 function move() { player_path_stack.push(currentPos);
var c=currentPos.getNextPos(dir); currentPos=c; this._x = left+this.currentPos.col*_stepSize; this._y = top+this.currentPos.row*_stepSize;
}
//返回上一步 function reback(){ if(player_path_stack.length>0){ var pre=player_path_stack.pop(); currentPos=pre; this._x = left+this.currentPos.col*_stepSize; this._y = top+this.currentPos.row*_stepSize; } if(boxes_path_stack.length>0 ){ var obj=boxes_path_stack.pop(); if(obj!=null){ var box=obj.box; var pos=obj.pos; box.pos=pos; box._x=box.left+box.pos.col*box._stepSize; box._y=box.top+box.pos.row*box._stepSize; } } } }
class Box extends MovieClip { var pos:Pos; var left:Number; var top:Number; var _wall:MovieClip; var player:MovieClip; var _stepSize:Number; var _targets:Array; var finished:Boolean;//此箱子是否到达目标地
function Box() { left = 0; top = 0; finished = false; _stepSize = 40; }
//设置目标地 function setTargets(t) { _targets = new Array(); _targets = t; }
//设置箱子的起始位置若罔闻 function setStartPos(p) { pos = new Pos(p.col, p.row); this._x = pos.col*_stepSize+left; this._y = pos.row*_stepSize+top; }
//移到下一个位置 function move() { var c = pos.getNextPos(player.dir); pos = c; this._x = left+pos.col*_stepSize; this._y = top+pos.row*_stepSize; gotTarget(); }
//是否到达目标地 function gotTarget() { for (var i = 0; i<_targets.length; i++) { if (this.pos.equals(_targets[i].pos)) { if (this.getDepth()>_targets[i].getDepth()) { this.swapDepths(_targets[i]); } this.finished = true; return; } } this.finished = false; }
//取得一下个位置上物体的类型 function getNextObject():String { //var obj:String; var nextPos:Pos; nextPos = pos.getNextPos(player.dir); if (_wall.brickMatrix[nextPos.row][nextPos.col] == 1) { return "WALL"; } for (var i = 0; i<player.boxes.length; i++) { if (player.boxes[i].pos.equals(nextPos)) { return "BOX"; } } return "NOTHING"; } }
class Matrix{ //用来存放游戏各关中初始界面 static var matrixs:Array;
//****************Manager.as************* //用来治理游戏 //所有的对象都入其中 //MovieClip类的子类 class Manager extends MovieClip { var wall:MovieClip; var player:MovieClip; var boxes:Array; var targets:Array; var stageNum:Number;//游戏的当前关数
function Manager() { _x=50; _y=50; }
//游戏初始化 function init(s) { stageNum = s;
boxes = new Array(); targets = new Array();
var brickMatrixT = new Matrix().getMatrix(stageNum);//根据关数从矩阵类中取得相应的矩阵