2 TileMapObject的使用
1 CCTMXObjectGroup的使用方法
為了取以下內容:
操作代碼如下:
| T27TileMapObject.h |
| #ifndef __T27TileMapObject_H__ #define __T27TileMapObject_H__ ? #include "cocos2d.h" #include "cocos-ext.h" #include "TBack.h" USING_NS_CC; USING_NS_CC_EXT; ? //格子地圖對象層 class T27TileMapObject :public TBack { public: ??? CREATE_FUNC(T27TileMapObject); ??? bool init(); ??? static CCScene* scene(); }; ? #endif ? ? |
| T27TileMapObject.cpp |
| #include "T27TileMapObject.h" ? CCScene* T27TileMapObject::scene() { ??? CCScene* scene = CCScene::create(); ??? T27TileMapObject * layer = T27TileMapObject::create(); ??? scene->addChild(layer); ??? return scene; } ? bool T27TileMapObject::init() { ??? TBack::init(); ? ??? CCTMXTiledMap * map = CCTMXTiledMap::create("MarioMap1.tmx"); ??? addChild(map); ??? ??? //這里的objects是MarioMap1.tmx中的一個key ??? CCTMXObjectGroup* objGroup = map->objectGroupNamed("objects"); ??? CCArray* objs = objGroup->getObjects(); ??? CCObject* obj; ? ??? CCTexture2D* texture = CCTextureCache::sharedTextureCache()->addImage("Mushroom0.png"); ??? CCSpriteFrame* frame = CCSpriteFrame::createWithTexture( ??????? texture, CCRectMake(0,0,texture->getContentSize().width / 4,texture->getContentSize().height)); ??? CCARRAY_FOREACH(objs, obj) ??? { ??????? //通過下面的方式獲得object中的參數信息 ??????? CCDictionary* dict = (CCDictionary*)obj; ??????? const CCString* name = dict->valueForKey("name"); ??????? const CCString* type = dict->valueForKey("type"); ??????? const CCString* x = dict->valueForKey("x"); ??????? const CCString* y = dict->valueForKey("y"); ??????? if (type->m_sString == "mushroom") ??????? { ??????????? //創建一個蘑菇怪Mushroom0.png ??????????? CCSprite* sprite = CCSprite::createWithSpriteFrame(frame); ??????????? map->addChild(sprite); ??????????? sprite->setPosition(ccp(x->intValue(),y->intValue())); ??????????? sprite->setZOrder(10000); ??????? } ??????? //如果object對象的名字是Birthday,那么將執行以下的操作 ??????? if (type->m_sString == "BirthPoint") ??????? { ??????????? CCSprite* sprite = CCSprite::createWithSpriteFrame(frame); ??????????? map->addChild(sprite); ??????????? sprite->setPosition(ccp(x->intValue(),y->intValue() - 16)); ??????????? //如果不設置這一句,馬里奧會在山后 ??????????? sprite->setZOrder(10000); ??????????? //設置錨點 ??????????? sprite->setAnchorPoint(ccp(0,0)); ??????? } ??? } ??? return true; } ? |
| 運行結果:
|
?
?
?
?
?
總結
以上是生活随笔為你收集整理的2 TileMapObject的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1CCTableView的使用,Tabl
- 下一篇: 索尼笔记本怎么进bios设置 如何进入索