3

如何创建具有相对路径的精灵?我的意思是我在Resources目录中有几个文件夹,例如:

  • sd
  • 高清
  • ……

此目录之一设置为应查找资源的位置

std::vector<std::string> resDirOrders;
 if (device is hd)
      resDirOrders.push_back("hd")
FileUtils::getInstance()->setSearchResolutionsOrder(resDirOrders);

现在在上面提到的每个目录中,我都有很多其他目录,例如:

  • intro_popup
  • outro_popup
  • 主菜单
  • 顶栏
  • 在游戏中
  • ……

在这些目录中,我放置图像。coin.png并且图像的名称可能会发生冲突,据我所知main_menu,其中是不同top_baringame图像。因此,我希望能够创建一个像这样的精灵:

Sprite::create("ingame/coin.png");
Sprite::create("top_bar/coin.png");

但它不起作用。它只是找不到文件 coin.png。

我应该如何解决这个问题?我在 Windows 上使用 cocos2d-x 3.0,但它也应该在 iOS 和 Android 上处理。

4

1 回答 1

5

文件夹结构必须是这样的:

Res/
----hd/
----sd/
----ingame/
    ----hd/
    ----sd/
----top_bar/
    ----hd/
    ----sd/

您应该将 Res 文件夹添加到项目中,并确保选中“为任何添加的文件夹创建文件夹引用”。

设置搜索路径和搜索解析顺序:

Size screenSize = Director::getInstance()->getOpenGLView()->getFrameSize();

std::vector<std::string> resDirOrders;

std::vector<std::string> searchPaths = FileUtils::getInstance()->getSearchPaths();
searchPaths.insert(searchPaths.begin(), "Res");
FileUtils::getInstance()->setSearchPaths(searchPaths);

if (screenSize.width > 1024) {
    resDirOrders.push_back("hd");
}
else{
    resDirOrders.push_back("sd");
}

FileUtils::getInstance()->setSearchResolutionsOrder(resDirOrders);

然后你可以创建精灵Sprite::create("ingame/coin.png");

于 2014-05-18T03:36:20.430 回答