原帖由 yong2212 于 8-3-2006 11:48 PM 发表
这里有一个小问题。。
我用几个array来design maze在frame 1。。。然后,randomly select a array and pass the array to frame 5 which contain code to generate the maze.
我的问题是为什么当我用button &qu ...
//--------- frame 1 --------
var line = new Array();
line[0] = new Array(
[1,2,1,1,1,1,1,1,1,1],
[1,0,1,0,0,0,0,0,0,1],
[1,0,1,0,1,0,1,1,0,1],
[1,0,1,1,1,0,1,1,0,1],
[1,0,0,0,0,0,0,1,0,1],
[1,0,1,0,1,1,1,1,0,1],
[1,0,1,0,0,0,1,1,0,1],
[1,0,1,1,1,0,1,1,0,1],
[1,0,0,0,1,0,1,1,0,3],
[1,1,1,1,1,1,1,1,1,1]
);
line[1] = new Array(
[1,1,1,1,1,1,1,1,1,1],
[1,0,1,0,0,0,0,0,0,1],
[1,0,1,0,1,0,1,1,0,1],
[1,0,1,1,1,0,1,1,0,1],
[1,2,0,0,0,0,0,1,0,1],
[1,0,1,0,1,1,1,1,0,1],
[1,0,1,0,0,0,1,1,0,1],
[1,0,1,1,1,0,1,1,0,1],
[1,0,0,0,1,0,1,1,0,3],
[1,1,1,1,1,1,1,1,1,1]
);
var rand = line[random(line.length)];
//-------------- frame 5 ----------------
//set the size of the image
var size:Number=20;
var surface:MovieClip = this.createEmptyMovieClip("maze",1);
//allocate the maze position
surface._x = 60;
surface._y = 90;
var mazewall:MovieClip = surface.createEmptyMovieClip("wall",2);
//attachmovie function
function attach(id,x,y)
{
var depth:Number = mazewall.getNextHighestDepth();
var box:MovieClip = mazewall.attachMovie(id,"b" + depth,depth);
box._x = x * size;
box._y = y * size;
}
//use to hold startpoint and endpoint
var startPoint:Object = new Object;
var endPoint:Object = new Object;
var playerPoint:Object = new Object;
var newRand = new Array();
//create wall,startpoint,endpoint and player
for (y=0;y<rand.length;y++){
newRand[y] = new Array();
for (x=0;x<rand[y].length;x++){
// rebuild the map data, so that we can use in solve:method
var type:Number = rand[y][x];
var o:Object = new Object();
o.type = type;
o.x = x;
o.y = y;
o.visited = false;
newRand[y][x] = o;
// checking the cell type and draw them
switch(type)
{
case 0:
break;
case 1:
//create walls
attach("box",x,y);
break;
case 2:
//create startpoint and player
attach("startpoint",x,y);
var p:MovieClip = surface.attachMovie("player","player2",surface.getNextHighestDepth());
p._x = x*size;
p._y = y*size;
playerPoint .x = x;
playerPoint .y = y;
playerPoint .mc = p;
startPoint = o;
break;
case 3:
//create endpoint
attach("endpoint",x,y);
endPoint = o;
break;
}
}
}
//Control "player"
function onKeyDown(){
var x:Number = playerPoint.x;
var y:Number = playerPoint.y;
//key control
switch (Key.getCode())
{
case Key.UP:
y = y - 1;
break;
case Key.DOWN:
y = y + 1;
break;
case Key.LEFT:
x = x - 1;
break;
case Key.RIGHT:
x = x + 1;
break;
}
//test the road can run or not
var road = newRand[y][x].type;
if(road != 1 && road != undefined)
{
playerPoint.mc._x = x * size;
playerPoint.mc._y = y * size;
playerPoint.x = x;
playerPoint.y = y;
}
//to the endpoint
if(road == 3)
{
trace("Exit");
//remove movieclip
removeMovieClip(surface);
gotoAndPlay(2);
}
}
//receive key control signal
Key.addListener(this);
//create a button call refreshBtn on stage
refreshBtn.onRelease = function() {
gotoAndPlay(2);
}
stop(); |