我在线发现了Actionscript-3的随机迷宫生成器。This is the maze I found。我稍微调整了一下,现在我需要找出一种方法来测试玩家是否撞墙了。我不知道如何测试玩家是否撞到了墙壁或者是否已经完成了开始和结束。这是我的代码。
gotoAndStop(3)
graphics.beginFill(0xffffff, 1);
graphics.drawRect(0, 0, 800, 600);
graphics.endFill();
graphics.beginFill(0x000000, 1);
graphics.drawRect(0, 0, 800, 600);
graphics.endFill();
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
var Player:player = new player();
addChild(Player)
var mazeW=26;
var mazeH=19;
var wall_size=20;
var startP = Point;
var finishP = Point;
var maze = new Array();
var moves = new Array();
var draw:Sprite = new Sprite();
var cells=mazeW*mazeH;
//this just makes the random starts and finishes
//var RNum:Number = Math.round(Math.random()*3);
//trace(RNum);
var RNum = 0
var pos=Math.floor(Math.random()*cells);
var visited=1;
for (var i=0; i<cells; i++) {
maze[i]=new Array(0,1,1,1);
}
if (RNum == 0){
startP = new Point(1, 1);
finishP = new Point(mazeW, mazeH);
graphics.beginFill(0xFF0000, 1);
graphics.drawRect(10, 10, 20, 20);
graphics.endFill();
graphics.beginFill(0x32CD32, 1);
graphics.drawRect(510, 370, 20, 20);
graphics.endFill();
Player.x = 520;
Player.y = 378;
Player.width = 14;
Player.height = 10;
}
if (RNum == 1){
graphics.beginFill(0xFF0000, 1);
graphics.drawRect(510, 10, 20, 20);
graphics.endFill();
graphics.beginFill(0x32CD32, 1);
graphics.drawRect(10, 370, 20, 20);
graphics.endFill();
}
if (RNum == 2){
graphics.beginFill(0xFF0000, 1);
graphics.drawRect(10, 370, 20, 20);
graphics.endFill();
graphics.beginFill(0x32CD32, 1);
graphics.drawRect(510, 10, 20, 20);
graphics.endFill();
}
if (RNum == 3){
graphics.beginFill(0xFF0000, 1);
graphics.drawRect(510, 370, 20, 20);
graphics.endFill();
graphics.beginFill(0x32CD32, 1);
graphics.drawRect(10, 10, 20, 20);
graphics.endFill();
}
maze[pos][0]=1;
while (visited<cells) {
var possible="";
if (((pos-mazeW)>=0) && (maze[pos-mazeW][0] == 0)) {
possible=possible+"N";
}
if (((pos+mazeW)<cells) && (maze[pos+mazeW][0] == 0)) {
possible=possible+"S";
}
if ((Math.floor(pos/mazeW) == Math.floor((pos+1)/mazeW)) && (maze[pos+1][0] == 0)) {
possible=possible+"E";
}
if ((Math.floor(pos/mazeW) == Math.floor((pos-1)/mazeW)) && (maze[pos-1][0] == 0)) {
possible=possible+"W";
}
if (possible) {
visited++;
moves.push(pos);
var way=possible.charAt(Math.floor(Math.random()*possible.length));
switch (way) {
case "N" :
maze[pos][1]=0;
maze[pos-mazeW][2]=0;
pos-=mazeW;
break;
case "S" :
maze[pos][2]=0;
maze[pos+mazeW][1]=0;
pos+=mazeW;
break;
case "E" :
maze[pos][3]=0;
maze[pos+1][4]=0;
pos++;
break;
case "W" :
maze[pos][4]=0;
maze[pos-1][3]=0;
pos--;
break;
}
maze[pos][0]=1;
} else {
pos=moves.pop();
}
}
addChild(draw);
draw.graphics.lineStyle(3,0x89A84A);
draw.graphics.moveTo(10,10);
var start_y=10-wall_size;
var start_x=0;
for (i=0; i<cells; i++) {
start_x+=wall_size;
if ((i%mazeW) == 0) {
start_y+=wall_size;
start_x=10;
}
if (maze[i][2]==1) {
draw.graphics.moveTo(start_x,start_y+wall_size);
draw.graphics.lineTo(start_x+wall_size,start_y+wall_size);
}
if (maze[i][3]==1) {
draw.graphics.moveTo(start_x+wall_size,start_y);
draw.graphics.lineTo(start_x+wall_size,start_y+wall_size);
}
}
draw.graphics.lineStyle(4,0x89A84A);
draw.graphics.moveTo(10,10);
draw.graphics.lineTo(10+wall_size*mazeW,10);
draw.graphics.lineTo(10+wall_size*mazeW,10+wall_size*mazeH);
draw.graphics.lineTo(10,10+wall_size*mazeH);
draw.graphics.lineTo(10,10);
我想过试图欺骗线条的颜色,并试图用实例名称生成墙壁,但从未弄明白。有什么想法吗?
编辑:我不能使用命中测试对象,因为这些线不是符号,是用于检测碰撞的任何其他方法