cocos2d-x TiledMap获取tiled属性:tiledID、旋转以及翻转信息

前端之家收集整理的这篇文章主要介绍了cocos2d-x TiledMap获取tiled属性:tiledID、旋转以及翻转信息前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


针对地图TiledMap工具地形刷出的图块进行属性获取方法

这块也是找了半天,最后通过TMXLayer类源码查询获得结果,在此记录下


工具操作:

1、按x,y键进行tiled对应的x,y轴翻转

2、按z键进行tiled旋转(每次90)


代码获取地图块信息

实现方法如下:

//获取地图层0
    auto ground1Layer = m_pMap->getLayer("ground1");
    
    Size size1 = ground1Layer->getLayerSize();
    for (int i = 0; i < size1.width; i ++) {
        for (int j = 0; j < size1.height; j ++) {
            Vec2 pos = Vec2(i,j);
            TMXTileFlags currentFlags;
            uint32_t gid = ground1Layer->getTileGIDAt(pos,¤tFlags);
            // if GID == 0,then no tile is present
            if (gid && currentFlags)
            {
                //获取翻转和旋转信息
                if (currentFlags & kTMXTileDiagonalFlag)
                {
                    // put the anchor in the middle for ease of rotation.
                    
                    int flag = currentFlags & (kTMXTileHorizontalFlag | kTMXTileVerticalFlag );
                    
                    // handle the 4 diagonally flipped states.
                    if (flag == kTMXTileHorizontalFlag)
                    {
                        log("旋转90");
                    }
                    else if (flag == kTMXTileVerticalFlag)
                    {
                        log("旋转270");
                    }
                    else if (flag == (kTMXTileVerticalFlag | kTMXTileHorizontalFlag) )
                    {
                        log("旋转90 && x轴翻转");
                    }
                    else
                    {
                        log("旋转270 && x轴翻转");
                    }
                }
                else
                {
                    if (currentFlags & kTMXTileHorizontalFlag)
                    {
                        log("x轴翻转");
                    }
                    
                    if (currentFlags & kTMXTileVerticalFlag)
                    {
                        log("y轴翻转");
                    }
                }

                
            }
        }

猜你在找的Cocos2d-x相关文章