创建地图可以使用一个二维数组来创建地图上的各种素材。
两次for循环,获取他地形的x和y的坐标。用一组key值来确定是什么类型
代码如下:
TankWar.prototype.createMap = function (id) {console.log(this.application)this.mapContainer = new PIXI.Container();this.mapContainer.position.set(14,10);if(this.currentStage !=0){const currentStageRow = stage[`stage${this.currentStage}`];for(let row =0;row<currentStageRow.length;row++){const currentStageColumn = stage[`stage${this.currentStage}`][row];console.log(currentStageColumn)for(let col = 0;col<currentStageColumn.length;col++){if(currentStageColumn[col] === 1) {const wall = new PIXI.Sprite(id.textures['map-dixing-qiang.png']);wall.position.set( col * 16,row * 16);wall.scale.set(2,2);wall.anchor.set(0.5,0.5);this.mapContainer.addChild(wall);console.log(currentStageColumn[col])console.log(wall.x,wall.y)}if(currentStageColumn[col] === 2){const grass = new PIXI.Sprite(id.textures['map-dixing-cao.png']);wall.position.set( col * 16,row * 16);wall.scale.set(2,2);wall.anchor.set(0.5,0.5);this.mapContainer.addChild(wall);console.log(currentStageColumn[col])console.log(wall.x,wall.y)}if(currentStageColumn[col] === 3){const wall = new PIXI.Sprite(id.textures['map-dixing-gang.png']);wall.position.set( col * 16,row * 16);wall.scale.set(2,2);wall.anchor.set(0.5,0.5);this.mapContainer.addChild(wall);console.log(currentStageColumn[col])console.log(wall.x,wall.y)}if(currentStageColumn[col] === 4){const river = new PIXI.Sprite(id.textures['map-dixing-heliu.png']);wall.position.set( col * 16,row * 16);wall.scale.set(2,2);wall.anchor.set(0.5,0.5);this.mapContainer.addChild(wall);console.log(currentStageColumn[col])console.log(wall.x,wall.y)}}}}this.application.stage.addChild(this.mapContainer);}
