import flash.display.Sprite; /*库中有个元件MC*/ var i:int = 0; var j:int = 0; //初始x、y坐标 var initX:Number = 0; var initY:Number = 0; //横向方块数目 var widthNumber:Number = 10; //纵向方块数目 var heightNumber:Number = 10; //画格子的总的宽度 var widthTotal:Number = 550; //画格子的总的高度 var heightTotal:Number = 400; //方块的宽度 var gridWidth:Number = widthTotal / widthNumber; //方块的高度 var gridHeight:Number = heightTotal / heightNumber; var lineSprite:Sprite = new Sprite(); lineSprite.graphics.lineStyle(1,0x000000); //lineSprite.graphics.beginFill(0xCCCCCC); //lineSprite.graphics.drawRect(0,0,widthTotal,heightTotal); //画线横线; for (i=0; i<heightNumber; i++) { lineSprite.graphics.moveTo(0,gridHeight * i); lineSprite.graphics.lineTo(widthTotal,gridHeight * i); lineSprite.graphics.endFill(); } //画线竖线; for (j=0; j<widthNumber; j++) { lineSprite.graphics.moveTo(gridWidth * j,0); lineSprite.graphics.lineTo(gridWidth * j,heightTotal); lineSprite.graphics.endFill(); } addChild(lineSprite); lineSprite.x = initX; lineSprite.y = initY; //************************************************** //给每个方块一个属性1 /***** var arrMC:Array = new Array ; var _arrMC:Array = new Array ; for (i=0; i<widthNumber; i++) { arrMC[i] = new Array ; _arrMC[i] = new Array ; for (j=0; j<heightNumber; j++) { arrMC[i][j]=new Array; _arrMC[i][j]=new Array; } } for (i=0; i<widthNumber; i++) { var ix:int = i for (j=0; j<heightNumber; j++) { var iy:int = j; _arrMC[ix][iy]=1; //根据方块的编号得到方块的x,y坐标 var targetX:Number = ix * gridWidth + initX+gridWidth/2; var targetY:Number = iy * gridHeight + initY+gridHeight/2; addMC(targetX,targetY); } } function addMC(targetX:Number,targetY:Number):void { var mc:MC=new MC; mc.x=targetX; mc.y=targetY; addChild(mc); } //根据编号求出坐标 position(10,10); function position(ix:uint,iy:uint):void { //根据方块的编号得到方块的x,y坐标 var targetX:Number = ix * gridWidth + initX; var targetY:Number = iy * gridHeight + initY; trace("("+ix+","+iy+")位置的格子坐标为("+targetX+","+targetY+")"); } //根据坐标求出编号 coordinates(550,400); function coordinates(targetX:uint,targetY:uint):void { //根据方块x,y坐标得到方块的编号 var ix:Number= (targetX-initX)/gridWidth; var iy:Number=(targetY-initY)/gridHeight; trace("坐标为("+targetX+","+targetY+")的格子位置为"+"("+ix+","+iy+")"); } trace(arrMC[0][0]); trace(arrMC.length); trace(arrMC[0].length); *****/ //************************************************** //给每个方块位置放置一个mc,并给每个已经有方块的位置一个属性1做标记 /****** var mc:MC; var arrMC:Array = new Array ; var _arrMC:Array = new Array ; for (i=0; i<widthNumber; i++) { arrMC[i] = new Array ; _arrMC[i] = new Array ; for (j=0; j<heightNumber; j++) { mc = new MC ; mc.x = i*gridWidth + initX+gridWidth/2; mc.y = j*gridHeight + initY+gridHeight/2; addChild(mc); //arrMC[i].push(mc); arrMC[i][j] = mc; _arrMC[i][j] = 1; } } ******/ //************************************************** //地图 /****** //此处是MovieClip所以后面可以给它赋属性p=1 var field:MovieClip=new MovieClip(); addChild(field); var arrMC:Array = new Array ; var _arrMC:Array = new Array ; init(field); function init(field:MovieClip):void { for (var y:uint=0; y<heightNumber; y++) { if (arrMC[y] == undefined) { arrMC[y] = new Array(); _arrMC[y] = new Array(); } for (var x:uint=0; x<widthNumber; x++) { var mc:MC=new MC; //arrMC[y][x]=mc; arrMC[y].push(field.addChildAt(mc, field.numChildren)); _arrMC[y][x] = 1; arrMC[y][x].x = y*gridWidth + initX+gridWidth/2; arrMC[y][x].y = x*gridHeight + initY+gridHeight/2; //arrMC[y][x].p=1; } } trace("allNode[3]--------"+arrMC[3]); //trace("allNode[3][0]--------"+arrMC[3][0].p);输出1 field.x = initX; field.y = initY; } ******/