TiledMap 使用
代码:
void HelloWorld::testTileMap(){
CCTMXTiledMap* map= CCTMXTiledMap::create("Untitled.tmx");//获取tmx文件
map->setPosition(ccp(this->getContentSize().width/2-100, this->getContentSize().height/2-100));
this->addChild(map);
// 所有的地图默认是失真的,如果你想创建非失真砖块,你需要添加入下代码:
//遍历所有的层(失真精灵管理者),并且设置他们为非失真
CCArray * pChildrenArray = map->getChildren();
CCSpriteBatchNode* child = NULL;
CCObject* pObject = NULL;
CCARRAY_FOREACH(pChildrenArray, pObject)
{
child = (CCSpriteBatchNode*)pObject;
if(!child)
break;
child->getTexture()->setAntiAliasTexParameters();
}
CCTMXLayer* layer1=map->layerNamed("layer1");//获取map里面的图层,
CCSprite* sp1=layer1->tileAt(ccp(3, 3));//获取图层上砖块的
CCSprite* sp2=layer1->tileAt(ccp(1, 0));
layer1->removeChild(sp1, true);//移除砖块
layer1->removeChild(sp2, true);
layer1->removeTileAt(ccp(9, 1));//根据坐标移除砖块
//map->removeChild(layer1);
unsigned int m_gid = layer1->tileGIDAt(ccp(5,5));//获取一个砖块A的GID
layer1->setTileGID(m_gid, ccp(2,7));//设定特定坐标为砖块A
// 遍历一个layer
// CCSize s = layer1->getLayerSize();
// for( int x=0; x<s.width;x++) {
// for( int y=0; y< s.height; y++ ) {
// unsigned int tmpgid = layer1->tileGIDAt(ccp(x,y));
// layer1->setTileGID(tmpgid+1,ccp(x,y));
// }
// }
//MARK: ==
CCTMXObjectGroup* group =map->objectGroupNamed("obj111");//获取对象组
CCDictionary* dic = group->objectNamed("yang");//获取对象组中的对象
int x=dic->valueForKey("x")->intValue();//获取x坐标
int y=dic->valueForKey("y")->intValue();//获取y坐标
CCSprite* pSprite = CCSprite::create("CloseNormal.png");
map->addChild(pSprite);//往map上添加一个精灵
pSprite->setPosition(ccp(x, y));//设置这个精灵的位置
// // 对象层 3.0以后使用方法
// TMXObjectGroup* pipeGroup = map->objectGroupNamed("pipe");
// //得一个
// ValueMap pipe_1 = pipeGroup->objectNamed("pipe_1");
// std::string pipeStr = pipe_1["name"].asString();
// float pipeX = pipe_1["x"].asFloat();
// float pipeY= pipe_1["y"].asFloat();;
// float pipeWidht = pipe_1["width"].asFloat();
// float pipeHeight = pipe_1["height"].asFloat();
CCDictionary* dic2 = group->objectNamed("xuan");
int x2=dic2->valueForKey("x")->intValue();
int y2=dic2->valueForKey("y")->intValue();
CCSprite* pSprite2 = CCSprite::create("CloseNormal.png");
map->addChild(pSprite2);
pSprite2->setPosition(ccp(x2, y2));
CCTMXObjectGroup* g2=map->objectGroupNamed("obj222");
CCDictionary* dic4=g2->objectNamed("uuu");
int x4 = dic4->valueForKey("x")->intValue();
int y4 = dic4->valueForKey("y")->intValue();
CCSprite* pSprite4 = CCSprite::create("CloseNormal.png");
map->addChild(pSprite4);
pSprite4->setPosition(ccp(x4, y4));
CCDictionary* dic5=g2->objectNamed("ooo");
int x5 = dic5->valueForKey("x")->intValue();
int y5 = dic5->valueForKey("y")->intValue();
CCSprite* pSprite5 = CCSprite::create("CloseNormal.png");
map->addChild(pSprite5);
pSprite5->setPosition(ccp(x5, y5));
}
效果图: