cocos2dx 读取tilemap objectgroup 属性为空问题

我的版本是2.0rc  读取objectgroup 的属性的时候 出现了:http://www.cocos2d-x.org/boards/6/topics/13525?r=13704#message-13704 里面提到的问题

提问的楼主提出了 用tinyxml解决读取的问题 挺好的

这里转下他的代码:

if (pBuffer)
{
        //Load data
        ///////////////////////////////////////////////////////////////////////////////////////////////////
        tinyxml2::XMLDocument doc;
        doc.Parse((const char*)pBuffer);
tinyxml2::XMLElement* map = doc.FirstChildElement("map");
        if ( map )
{
            tinyxml2::XMLElement* objects = map->FirstChildElement("objectgroup");
            if ( objects )
    {
                tinyxml2::XMLElement* firstChild = objects->FirstChildElement();
                if ( firstChild )
        {
                    tinyxml2::XMLElement* nextChild = firstChild;
                    while ( nextChild != NULL )
            {
                        std::string elementName = nextChild->Name();
                        if ( elementName == std::string("object") )
                {
                            onEntityElement(nextChild, scene);
                        }
nextChild = nextChild->NextSiblingElement();
                    }
                }
            }
        }
        ///////////////////////////////////////////////////////////////////////////////////////////////////
    }
@
@
void MapLoader::onEntityElement(tinyxml2::XMLElement* object, cocos2d::CCLayer* scene) {
//Position and type
float x = object->FloatAttribute("x");
float y = object->FloatAttribute("y");

float width = object->FloatAttribute("width");
    float height = object->FloatAttribute("height");
//x = (x)-m_map->getMapSize().width*32;
    y = (y)+m_map>getMapSize().height*32;
CCPoint pos = ccp(x,y);
std::string type = object->Attribute("type");
//Attributes
    std::vector<SObjectParam> params;
    tinyxml2::XMLElement* props = object->FirstChildElement("properties");
    if ( props )
{
        tinyxml2::XMLElement* firstChild = props->FirstChildElement();
        if ( firstChild )
    {
            tinyxml2::XMLElement* nextChild = firstChild;
            while ( nextChild != NULL )
        {
                if ( std::string(nextChild->Name()) == std::string("property") )
            {
                    SObjectParam param;
                    param.m_name = nextChild->Attribute("name");
                    param.m_value = nextChild->Attribute("value");
                    params.push_back(param);
                }
nextChild = nextChild->NextSiblingElement();
            }
        }
    }
//Create entity
    GameObject* obj = GameObjectFactory::createObject(type, pos, params, width, height);
    if ( obj )
{
        m_map->addChild(obj);
    }
}@

不过还是希望 2.0能解决读取xml objectgroup 属性为空的问题 因为 1.01读取是没有问题的

posted @ 2012-08-01 11:21  dodohua  阅读(1230)  评论(0编辑  收藏  举报