当前位置:   article > 正文

cocoa2d-x tiled map添加tile翻转功能

tiledmap 镜像翻转

今天用cocos2d-1.0.1-x-0.9.2来测试tiled map的功能,结果发现翻转过的tile都不见了,调试代码发现原来代码里不支持,没有对x,y翻转作处理,结果翻转过的tile导致数值多大没有被现实出来。

( 注:最新的Tiled map已经支持flip x和flip y,选中tile按键盘上x键,y键即可,其他旋转功能他们正在添加中。。。最新版本可以从http://www.mapeditor.org/下载)

修改代码实现此功能, CCTMXLayer.cpp文件里针对appendTileForGID方法作出修改,上代码:


  1. // used only when parsing the map. useless after the map was parsed
  2. // since lot's of assumptions are no longer true
  3. CCSprite * CCTMXLayer::appendTileForGID(unsigned int gid, const CCPoint& pos)
  4. {
  5. //lancer add for tile flip //setFlipX
  6. int flip = gid>>28;
  7. int tileid = gid &0x0FFFFFFF;
  8. gid = tileid;
  9. //end
  10. CCRect rect = m_pTileSet->rectForGID(gid);
  11. rect = CCRectMake(rect.origin.x / m_fContentScaleFactor, rect.origin.y / m_fContentScaleFactor, rect.size.width/ m_fContentScaleFactor, rect.size.height/ m_fContentScaleFactor);
  12. int z = (int)(pos.x + pos.y * m_tLayerSize.width);
  13. if( ! m_pReusedTile )
  14. {
  15. m_pReusedTile = new CCSprite();
  16. m_pReusedTile->initWithBatchNode(this, rect);
  17. }
  18. else
  19. {
  20. m_pReusedTile->initWithBatchNode(this, rect);
  21. }
  22. m_pReusedTile->setPosition(positionAt(pos));
  23. m_pReusedTile->setVertexZ((float)vertexZForPos(pos));
  24. m_pReusedTile->setAnchorPoint(CCPointZero);
  25. m_pReusedTile->setOpacity(m_cOpacity);
  26. //lancer add for tile flip //setFlipX
  27. cocos2d::CCLog("---gid=%d, flip sign=%d, tileid=%d\n", gid, flip, tileid);
  28. #define TILE_FLIP_X 8
  29. #define TILE_FLIP_Y 4
  30. if(flip & TILE_FLIP_X){
  31. cocos2d::CCLog("set flip x\n");
  32. m_pReusedTile->setFlipX(true);
  33. }
  34. if(flip & TILE_FLIP_Y){
  35. cocos2d::CCLog("set flip y\n");
  36. m_pReusedTile->setFlipY(true);
  37. }
  38. //end
  39. // optimization:
  40. // The difference between appendTileForGID and insertTileforGID is that append is faster, since
  41. // it appends the tile at the end of the texture atlas
  42. unsigned int indexForZ = m_pAtlasIndexArray->num;
  43. // don't add it using the "standard" way.
  44. addQuadFromSprite(m_pReusedTile, indexForZ);
  45. // append should be after addQuadFromSprite since it modifies the quantity values
  46. ccCArrayInsertValueAtIndex(m_pAtlasIndexArray, (void*)z, indexForZ);
  47. return m_pReusedTile;
  48. }

PS:转载请保留以下信息
Author:smilelance


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/305067
推荐阅读
相关标签
  

闽ICP备14008679号