上面的几个数组Maze,Room,Cell之间其实有一个包含与被包含的关系,因为Cell被加入到Room中,而Room又被加入到了Maze中,所以Maze是最外层的数组,其包含了数组Room而数组Room又包含了Cell,这样其实就建立了一个三维数组,Maze代表了整个的迷宫,其元素Room代表了迷宫中的房间,而room中的元素cell则代表了房间里的墙壁,这样层层嵌套,有利于实现下面的撞墙判断脚本。
if (Maze[roomNum][cellNum][wallNum] == false) {
continue;//如果迷宫中的第roomNum个房间里的第cellNum堵墙的值为faulse,则继续进行判断,直到值为true
}
//判断Maze数组中编号为[roomNum][cellNum][wallNum]的元素值是否为假,如果为
//假,进行下一步循环
x = Maze[roomNum][cellNum][4];
y = Maze[roomNum][cellNum][5];
//分别设置x,y为第roomNum号房间的第cellNum堵墙的第4个和第5个元素
if (wallNum == 0 and x == mazeW-1) {
continue;
} else if (wallNum == 1 and y == mazeH-1) {
continue;
} else if (wallNum == 2 and x == 0) {
continue;
} else if (wallNum == 3 and y == 0) {
continue;
}
//对变量进行判断,然后执行相应的操作
oppsCell = new Array();
//定义新数组oppsCell
if (wallNum == 0) {
oppsCell = fun_findcell(x+1, y);
} else if (wallNum == 1) {
oppsCell = fun_findcell(x, y+1);
} else if (wallNum == 2) {
oppsCell = fun_findcell(x-1, y);
} else if (wallNum == 3) {
oppsCell = fun_findcell(x, y-1);
}
// 根据墙壁编号的不同,分别调用参数不同的函数,fun_findcell为自定义函数
if (oppsCell[0] == roomNum) {
continue;
}
if (wallNum == 0) {
oppsWall = 2;
} else if (wallNum == 1) {
oppsWall = 3;
} else if (wallNum == 2) {
oppsWall = 0;
} else if (wallNum == 3) {
oppsWall = 1;
}
// 打开单元格中的一堵墙
Maze[roomNum][cellNum][wallNum] = false;
Maze[oppsCell[0]][oppsCell[1]][oppsWall] = false;
Maze[roomNum] = Maze[roomNum].concat(Maze[oppsCell[0]]);
Maze.splice(oppsCell[0], 1);
//用四个墙壁将空间封闭
if (Maze.length == 1) {
break;
}
}
function fun_findcell (a, b) {
reCell = new Array();
for (i=0; i
for (j=0; j
if (Maze[i][j][4] == a && Maze[i][j][5] == b) {
reCell.push(i, j);
return reCell;
}
}
}
}
//自定义函数fun_findcell,用来寻找没有封闭的单元格
在第5祯添加如下AS;
Maze=Maze[0];
cellW=30;
wn=0;
//定义三个变量
for (i=0; i
cellX = Maze[i][4];
cellY = Maze[i][5];
for (j=0; j<4; j++) {
if (Maze[i][j] == true) {
_root.attachMovie("w"+j, "ww"+wn, wn);
eval("ww"+wn)._x = 105+cellW*cellX;
eval("ww"+wn)._y = 35+cellW*cellY;
wn++;
}
}
}
//控制游戏主角在迷宫中运动,并对路线进行判断
man._x=105;
man._y=35;
//设置游戏主角的坐标
man.gotoAndStop(1);
//主角处于游戏进行状态
exit._x=105+11*cellW;
exit._y=35+11*cellW;
//设置迷宫出口坐标
manX = 0;
manY = 0;
在第6祯和第7祯都添加AS; stop();,用以停止游戏进行,以便让玩家选择,同时设置第7祯的祯标签为playagain,接下来给重玩按钮设置AS;
on (release) {
for(i=0;i
removeMovieClip("ww"+i);
}
gotoAndPlay (1);
}
//将游戏中复制出的迷宫墙壁全部清除掉,然后回到游戏开始重新构造迷宫地图