LayaAir 在TiledMap 中 插入精灵

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

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
}]
 
问题解决
 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值