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
- 下一篇: Microsoft公司的匈牙利法命名规则