Cocos2d "tile objects" are not supported yet.
so I implemented "Tile Object as sprite" in Cocos2d-x.
fixed just few line.
I hope this source is helpful to someone.
I include sample source.
becarful this will change your lib.
////////////CCTMXXMLParser.cpp
else if(elementName == "object")
{
.............................
.......................................
line 427:
////////////////////////////////////////////
// gid setter
/////////////////////////////////////////////
int gid = atoi(valueForKey("gid",attributeDict));
key = "gid";
sprintf(buffer,"%d",gid);
value = new CCString(buffer);
dict->setObject(value,key);
value->release();
////////////////////////////////////////////
int x = atoi(valueForKey("x", attributeDict)) + (int)objectGroup->getPositionOffset().x;
key = "x";
sprintf(buffer, "%d", x);
value = new CCString(buffer);
dict->setObject(value, key);
value->release();
...............
...............
}
////////////////CCTMXTiledMap.cpp
bool CCTMXTiledMap::initWithTMXFile(const char *tmxFile)
{
.......................................
.......................................
Line 106:
////////////// Insert Tile Object Layer
CCTMXObjectGroup *objectgroup = NULL;
CCMutableArray::CCMutableArrayIterator itr;
for (itr = m_pObjectGroups->begin(); itr !=m_pObjectGroups->end();++itr)
{
objectgroup = *itr;
CCNode *child = parseObject(objectgroup,mapInfo);
addChild((CCNode*)child,idx,idx);
m_pTMXLayers->setObject((CCTMXLayer*)child,objectgroup->getGroupName());
}
////////////////////
}
}
return true;
}
//////////////////////////////////////////////////////////
// tilesetForObject which tileset used in objectgroup
// Objectgroup에 사용된 타일셋 반환
//////////////////////////////////////////////////////////
CCTMXTilesetInfo* CCTMXTiledMap::tilesetForObject(CCTMXObjectGroup *objectInfo, CCTMXMapInfo *mapInfo){
CCMutableArray * tilesets = mapInfo->getTilesets();
if (tilesets && tilesets->count()>0)
{
CCTMXTilesetInfo *tileset = NULL;
CCMutableArray::CCMutableArrayRevIterator rit;
for (rit = tilesets->rbegin(); rit != tilesets->rend(); ++rit)
{
tileset = *rit;
if (tileset)
{
for(int k =0; kgetObjects()->count();k++){
CCStringToStringDictionary *object = objectInfo->getObjects()->getObjectAtIndex(k);
int gid = object->objectForKey("gid")->toInt();
if( gid != 0 )
{
// Optimization: quick return
// if the layer is invalid (more than 1 tileset per layer) an assert will be thrown later
if( gid >= tileset->m_uFirstGid )
return tileset;
}
}
}
}
}
CCLOG("cocos2d: Warning: TMX ObjectGroup '%@' has no tile Object",objectInfo->getGroupName());
return NULL;
}
/////////////////////////////////////////////////////////
// Parsing Object
// Object 타일 파싱
/////////////////////////////////////////////////////////
CCNode * CCTMXTiledMap::parseObject(CCTMXObjectGroup *objectInfo, CCTMXMapInfo *mapInfo)
{
CCTMXTilesetInfo *tileset = tilesetForObject(objectInfo, mapInfo);
CCNode *layer = CCNode::node();
if(tileset == NULL) {
return layer;
}
CCSprite *obj;
CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage(tileset->m_sSourceImage.c_str());
tileset->m_tImageSize = texture->getContentSize();
for(int k =0; kgetObjects()->count();k++){
CCStringToStringDictionary *object = objectInfo->getObjects()->getObjectAtIndex(k);
int gid = object->objectForKey("gid")->toInt();
if(gid)
{
obj = new CCSprite();
obj->autorelease();
int x = object->objectForKey("x")->toInt();
int y = object->objectForKey("y")->toInt();
CCRect rect = tileset->rectForGID(gid);
obj->initWithTexture(texture,rect);
obj->setAnchorPoint(ccp(0,0));
obj->setPosition(ccp(x,y));
layer->addChild(obj);
}
}
return layer;
}
tmx.jpg (75.7 KB)
use.jpg (66.4 KB)
cocos2d-1.0.0-x-0.9.0.zip (1327.7 KB)