Tile Based Games 遊戲程式中,會使用陣列方式設定與顯示遊戲地圖,在這邊撰寫一個簡

單的繪製程式,繪製 Map。

2010-09-12_092303 

嵌入圖片

[Embed(source = '../Lib/Tile1.PNG')]
private var Tile1:Class;
[Embed(source = '../Lib/Tile2.PNG')]
private var Tile2:Class;
[Embed(source = '../Lib/Tile3.PNG')]
private var Tile3:Class;

Map 陣列,Map陣列數值0 顯示 Tile1  1顯示 Tile2 2顯示Tile3

private var  Map:Array = [
                          [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                          [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], 
                          [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], 
                          [1, 0, 1, 1, 1, 0, 0, 0, 0, 1], 
                          [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], 
                          [1, 0, 0, 0, 0, 0, 1, 0, 0, 1], 
                          [1, 0, 0, 0, 0, 0, 1, 0, 0, 1],
                          [1, 0, 0, 0, 0, 0, 1, 0, 0, 1], 
                          [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], 
                          [1, 1, 1, 1, 2, 2, 1, 1, 1, 1],                          
                          ];    

使用 for 迴圈繪製 Map

var tempTile:Bitmap;

            for (var i:int = 0; i < 10; i++ ) {
                
                for (var j:int = 0; j < 10; j++ ) {
                    
                    if (Map[i][j] == 0) {
                        
                        tempTile = new Tile1();
                        tempTile.x=j*19
                        tempTile.y = i * 19
                        addChild(tempTile)
                        
                    }else if (Map[i][j] == 1){
                        tempTile = new Tile2();
                        tempTile.x=j*19
                        tempTile.y = i * 19
                        addChild(tempTile)
                        
                    }else if (Map[i][j] == 2){
                        tempTile = new Tile3();
                        tempTile.x=j*19
                        tempTile.y = i * 19
                        addChild(tempTile)
                    }
                    
                }
            }

 

檔案下載:map.rar ( FlashDevelop AS3 Project )

參考網站:http://www.tonypa.pri.ee/tbw/

iammic 發表在 痞客邦 PIXNET 留言(0) 人氣()