ccc tiledmap 获取元素属性

cc.Class({
    extends: cc.Component,

    properties: {
        elementLable: {
            default: null,
            type   : cc.Label
         },
        map: {
            default:null,
            type   :cc.TiledMap
        },
    },

    // use this for initialization
    onLoad: function () {
        
        var self=this
        
        cc.eventManager.addListener({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,

            //开始
            onTouchBegan: function(touch, event) {
        
                var pos=touch.getLocation()
                self.elementLable.string =self.checkElement(self,pos)
                return true; // don't capture event
            },

            //移动
            onTouchMoved: function(touch, event) {
                var pos=touch.getLocation()
                self.elementLable.string =self.checkElement(self,pos)

            },
            
            //结束
            onTouchEnded: function(touch, event) {
                console.log("end")
            }
        }, self.node);
    },
    
    checkElement:function (self,pos) {
        var worldLayer =self.map.getLayer("World")
        var tilepos= self.tileCoordForPosition(self.map,pos)
        var gid=worldLayer.getTileGIDAt(tilepos);
         
        return gid-1
    },
    
    
    tileCoordForPosition:function(map,position)
    {
        var x = position.x /map.getTileSize().width;
        var y = ((map.getMapSize().height * map.getTileSize().height) - position.y) / map.getTileSize().height;
        
        return cc.p(parseInt(x), parseInt(y));
    },
    
    
    
    onMapLoad:function(err)
    {
        //有错误退出
        if (err) return;
        console.log("map load")
        
        var pos=cc.p(288,321);
        var tile=this.tileCoordForPosition(this.map,pos);
        console.log(tile.x);
        console.log(tile.y);
    },

});

原文地址:https://www.cnblogs.com/yufenghou/p/5439828.html