LayaAir 在TiledMap 中 插入精灵

来源:互联网 发布:ezbuy和淘宝什么关系 编辑:程序博客网 时间:2024/06/05 09:30

以下这段代码的功能是 打开一个地图 然后 插入一个精灵在最上方的一个图层中。这个图层可以是对象层 也可以是格子

class GameInfo{    private tiledMap: Laya.TiledMap;    private x:number=6;    private y:number=3;    private player:Laya.GridSprite;    private sp:Laya.Sprite;    constructor(){        console.info("start");        this.init();    }    init():void{        Laya.init(800, 700, Laya.WebGL);        Laya.loader.load("res/atlas/images.atlas",Laya.Handler.create(this,this.onLoaded),null,Laya.Loader.ATLAS);                    }    onLoaded(){        console.info("onLoaded");        this.tiledMap = new Laya.TiledMap();        this.tiledMap.createMap("desert.json", new Laya.Rectangle(0, 0, Laya.stage.width, Laya.stage.height), Laya.Handler.create(this,this.loadedMap));                    }    loadedMap(){        this.sp=new Laya.Sprite();        this.sp.graphics.drawRect(0,0,100,100,"#FF0000");        var mapLayer = this.tiledMap.getLayerByIndex(1);        console.info(mapLayer.layerName);        this.sp.pos(0,0);        mapLayer._childs[0].addChild(this.sp);            }   }// 程序入口new GameInfo();


遇到的问题1
把精灵插入地图时 如果代码这样写就会报错 
mapLayer.addChild(this.sp); 
必须要这样写才可以
mapLayer._childs[0].addChild(this.sp);
 
你问我为啥把精灵插入到 地图中, 这样做 地图滚动时 精灵也会跟着滚动
 
遇到的问题2
 
当地图的 图层中的JSON数据 data 属性为 空数组时 使用 以上方法就会失败
{
"data":[],  //这样就会失败
"draworder":"topdown",
-----
}
 
如果是对象层 
objects:[] //这样也会失败
所以说当层里没有任何数据时 当你插入精灵就不会显示
于是我在这个对象层中 加了一个 大小位置都为0的 对象

[
{
"height":0,
"name":"",
"properties":
{
},
"rotation":0,
"type":"",
"visible":true,
"width":0,
"x":0,
"y":0
}]
 
问题解决
 

在这里记录我曾经遇到的那些问题
 
也希望这篇文章 可以帮助到大家 避开这些弯路! 

原创粉丝点击