cocos2dx 3.x Tiled的程序控制

前端之家收集整理的这篇文章主要介绍了cocos2dx 3.x Tiled的程序控制前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

From: http://blog.csdn.net/lengxue789/article/details/38399995

1. Tiled 对象层

TMXObjectGroup 对象存放了对象层的所有对象,通过getObjectGroup函数获取指定名称的对象层,还可以通过getObject获得具体的对象。

[html] view plain @L_403_2@ print ?
  1. //加载对象层
  2. autoobjGroup=map->getObjectGroup("objects");
  3. //加载玩家坐标
  4. autoplayerPointDic=objGroup->getObject("PlayerPoint");
  5. floatplayerX=playerPointDic.at("x").asFloat();
  6. floatplayerY=playerPointDic.at("y").asFloat();

2. 地图随主角的滚动

每当主角的坐标改变时,地图坐标随着改变。

[html] view plain @L_403_2@ print ?
  1. autoparent=(Layer*)getParent();
  2. //地图方块的数量
  3. SizemapTiledNum=m_map->getMapSize();
  4. //地图单个格子的大小
  5. SizetiledSize=m_map->getTileSize();
  6. //地图的大小
  7. SizemapSize=Size::Size(mapTiledNum.width*tiledSize.width,mapTiledNum.height*tiledSize.height);
  8. //屏幕大小
  9. SizevisibleSize=Director::getInstance()->getVisibleSize();
  10. //主角坐标
  11. autospritePos=getPosition();
  12. //如果主角坐标小于屏幕的一半,则取屏幕中点坐标,否则取主角的坐标
  13. floatx=MAX(spritePos.x,visibleSize.width/2);
  14. floaty=MAX(spritePos.y,visibleSize.height/2);
  15. //如果X、Y的坐标大于右上角的极限值,则取极限值的坐标(极限值是指不让地图超过屏幕造成出现黑边的极限坐标)
  16. x=MIN(x,mapSize.width-visibleSize.width/2);
  17. y=MIN(y,mapSize.height-visibleSize.height/2);
  18. //目标点
  19. autodestPos=Point(x,y);
  20. //屏幕中点
  21. autocenterPos=Point(visibleSize.width/2,visibleSize.height/2);
  22. autoviewPos=centerPos.operator-(destPos);
  23. parent->setPosition(viewPos);

3. 主角遇到障碍物如何处理

添加障碍物,参考笨木头的博客http://www.benmutou.com/archives/32

判断前面是否有障碍物,3.x的版本和2.0版本不同。

[html] view plain @L_403_2@ print ?
  1. voidPlayer::setTagPosition(intx,inty){
  2. SizespriteSize=m_sprite->getContentSize();
  3. autodstPos=Point(x+spriteSize.width/2,y);
  4. //获取当前主角前方坐标在地图中的格子位置
  5. autotiledPos=tileCoordForPosition(Point(dstPos.x,dstPos.y));
  6. inttiledGid=Meta->getTileGIDAt(tiledPos);
  7. if(tiledGid!=0)
  8. {
  9. autopropertiesDict=m_map->getPropertiesForGID(tiledGid).asValueMap();
  10. if(!propertiesDict.empty()){
  11. autoprop=propertiesDict["Collidable"].asString();
  12. if("true"==prop){
  13. x-=1;
  14. y-=1;
  15. }
  16. autopropStar=propertiesDict["star"].asString();
  17. if("true"==propStar){
  18. autobarrier=m_map->getLayer("barrier");
  19. barrier->removeTileAt(tiledPos);
  20. }
  21. autopropWin=propertiesDict["win"].asString();
  22. if("true"==propWin){
  23. Director::getInstance()->replaceScene(WinScene::createScene());
  24. }
  25. }
  26. }
  27. Entity::setTagPosition(x,y);
  28. setViewPointByPlayer();
  29. }

getTileGIDAt 函数:通过指定的tile坐标获取对应的tile grid,也返回对应的tile flags 这个方法要求tile地图之前没有被释放掉(如,不能调用layer->releaseMap())

getPropertiesForGID函数通过GID获取对应的属性字典(properties dictionary)

asValueMap函数 将value转为valueMap,之后可以通过name得到属性值。

原文链接:https://www.f2er.com/cocos2dx/345079.html

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