3409p00e76 发表于 2016-5-18 10:01:28

cocos2dx备忘录 windows vs

  试图把公司里在windows vs下开发的程序直接在家里mac上跑
  最后发现使用tinyxml2的XMLDocument的loadFile方法无法加载到文件,代码如下

    tinyxml2::XMLDocument * root = new tinyxml2::XMLDocument;
root->LoadFile("actor.xml");
  然后就郁闷,vs上直接能加载的资源为什么在xcode下就没法加载了呢?
  于是跟踪CCSprite,查找cocos2dx是怎样加载资源的
  CCSprite下有个方法initWithFile,如下

bool CCSprite::initWithFile(const char *pszFilename, const CCRect& rect)
{
CCAssert(pszFilename != NULL, "");
CCTexture2D *pTexture = CCTextureCache::sharedTextureCache()->addImage(pszFilename);
if (pTexture)
{
return initWithTexture(pTexture, rect);
}
// don't release here.
// when load texture failed, it's better to get a "transparent" sprite then a crashed program
// this->release();
return false;
}
  可以看到去直接把文件名传给CCTextureCache去缓存资源的,那么继续断点进去看

std::string strTemp = CCFileUtils::sharedFileUtils()->fullPathForFilename(strPath);
  该方法引起了我的注意,原来资源在xcode模拟器中编译是编译的类似的路径:
/Users/rocklee/Library/Developer/Xcode/DerivedData/MoonSugar-amahwvhxkigcpscnzrkgnlhbkoni/Build/Products/Debug/MoonSugar.app/Contents/Resources/actor.xml
以上路径是cocos2dx的mac os项目,如果是ios项目那么路径是另外一条,但是原理相同。
而CCFileUtils就可以获取文件全路径的。
根据得到得路径再去加载文件就加载到啦。
另外,资源一定要add File to 项目中,如果不加到项目中是不会进行编译得。
页: [1]
查看完整版本: cocos2dx备忘录 windows vs