From: http://blog.csdn.net/lengxue789/article/details/38399995
1. Tiled 对象层
TMXObjectGroup 对象存放了对象层的所有对象,通过getObjectGroup函数获取指定名称的对象层,还可以通过getObject获得具体的对象。
- //加载对象层
- autoobjGroup=map->getObjectGroup("objects");
- //加载玩家坐标
- autoplayerPointDic=objGroup->getObject("PlayerPoint");
- floatplayerX=playerPointDic.at("x").asFloat();
- floatplayerY=playerPointDic.at("y").asFloat();
//加载对象层 auto objGroup = map->getObjectGroup("objects"); //加载玩家坐标 auto playerPointDic = objGroup->getObject("PlayerPoint"); float playerX = playerPointDic.at("x").asFloat(); float playerY = playerPointDic.at("y").asFloat();
2. 地图随主角的滚动
每当主角的坐标改变时,地图坐标随着改变。
- autoparent=(Layer*)getParent();
- //地图方块的数量
- SizemapTiledNum=m_map->getMapSize();
- //地图单个格子的大小
- SizetiledSize=m_map->getTileSize();
- //地图的大小
- SizemapSize=Size::Size(mapTiledNum.width*tiledSize.width,mapTiledNum.height*tiledSize.height);
- //屏幕大小
- SizevisibleSize=Director::getInstance()->getVisibleSize();
- //主角坐标
- autospritePos=getPosition();
- //如果主角坐标小于屏幕的一半,则取屏幕中点坐标,否则取主角的坐标
- floatx=MAX(spritePos.x,visibleSize.width/2);
- floaty=MAX(spritePos.y,visibleSize.height/2);
- //如果X、Y的坐标大于右上角的极限值,则取极限值的坐标(极限值是指不让地图超过屏幕造成出现黑边的极限坐标)
- x=MIN(x,mapSize.width-visibleSize.width/2);
- y=MIN(y,mapSize.height-visibleSize.height/2);
- //目标点
- autodestPos=Point(x,y);
- //屏幕中点
- autocenterPos=Point(visibleSize.width/2,visibleSize.height/2);
- autoviewPos=centerPos.operator-(destPos);
- parent->setPosition(viewPos);
auto parent = (Layer*)getParent(); //地图方块的数量 Size mapTiledNum = m_map->getMapSize(); //地图单个格子的大小 Size tiledSize = m_map->getTileSize(); //地图的大小 Size mapSize = Size::Size(mapTiledNum.width*tiledSize.width,mapTiledNum.height*tiledSize.height); //屏幕大小 Size visibleSize = Director::getInstance()->getVisibleSize(); //主角坐标 auto spritePos = getPosition(); //如果主角坐标小于屏幕的一半,则取屏幕中点坐标,否则取主角的坐标 float x = MAX(spritePos.x,visibleSize.width/2); float y = MAX(spritePos.y,visibleSize.height/2); //如果X、Y的坐标大于右上角的极限值,则取极限值的坐标(极限值是指不让地图超过屏幕造成出现黑边的极限坐标) x = MIN(x,mapSize.width - visibleSize.width/2); y = MIN(y,mapSize.height - visibleSize.height/2); //目标点 auto destPos = Point(x,y); //屏幕中点 auto centerPos = Point(visibleSize.width/2,visibleSize.height/2); auto viewPos = centerPos.operator-(destPos); parent->setPosition(viewPos);
3. 主角遇到障碍物如何处理
添加障碍物,参考笨木头的博客,http://www.benmutou.com/archives/32
判断前面是否有障碍物,3.x的版本和2.0版本不同。
- voidPlayer::setTagPosition(intx,inty){
- SizespriteSize=m_sprite->getContentSize();
- autodstPos=Point(x+spriteSize.width/2,y);
- //获取当前主角前方坐标在地图中的格子位置
- autotiledPos=tileCoordForPosition(Point(dstPos.x,dstPos.y));
- inttiledGid=Meta->getTileGIDAt(tiledPos);
- if(tiledGid!=0)
- {
- autopropertiesDict=m_map->getPropertiesForGID(tiledGid).asValueMap();
- if(!propertiesDict.empty()){
- autoprop=propertiesDict["Collidable"].asString();
- if("true"==prop){
- x-=1;
- y-=1;
- }
- autopropStar=propertiesDict["star"].asString();
- if("true"==propStar){
- autobarrier=m_map->getLayer("barrier");
- barrier->removeTileAt(tiledPos);
- }
- autopropWin=propertiesDict["win"].asString();
- if("true"==propWin){
- Director::getInstance()->replaceScene(WinScene::createScene());
- }
- }
- }
- Entity::setTagPosition(x,y);
- setViewPointByPlayer();
- }
void Player::setTagPosition(int x,int y){ Size spriteSize = m_sprite->getContentSize(); auto dstPos = Point( x + spriteSize.width/2,y); //获取当前主角前方坐标在地图中的格子位置 auto tiledPos = tileCoordForPosition(Point(dstPos.x,dstPos.y)); int tiledGid = Meta->getTileGIDAt(tiledPos); if (tiledGid != 0) { auto propertiesDict = m_map->getPropertiesForGID(tiledGid).asValueMap(); if (!propertiesDict.empty()) { auto prop = propertiesDict["Collidable"].asString(); if("true" == prop){ x -=1; y -=1; } auto propStar = propertiesDict["star"].asString(); if("true" == propStar){ auto barrier = m_map->getLayer("barrier"); barrier->removeTileAt(tiledPos); } auto propWin = propertiesDict["win"].asString(); if("true" == propWin){ Director::getInstance()->replaceScene(WinScene::createScene()); } } } Entity::setTagPosition(x,y); setViewPointByPlayer(); }
getTileGIDAt 函数:通过指定的tile坐标获取对应的tile grid,也返回对应的tile flags 这个方法要求tile地图之前没有被释放掉(如,不能调用layer->releaseMap())