【问题标题】:cocos2d-x 3.0 - Create sprite by relative pathcocos2d-x 3.0 - 通过相对路径创建精灵
【发布时间】:2014-07-06 03:25:30
【问题描述】:

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

  • sd
  • 高清
  • ....

其中一个目录被设置为应该查找资源的地方

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

现在在上述每个目录中,我都有很多其他目录,例如:

  • intro_popup
  • outro_popup
  • main_menu
  • top_bar
  • 游戏
  • ....

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

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

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

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

【问题讨论】:

    标签: c++ cocos2d-x cocos2d-x-3.0


    【解决方案1】:

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

    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");创建精灵

    【讨论】:

    • 谢谢你。你救了我的命。为了理解这项工作的原因,请确认setSearchResolutionsOrder 是一个插入到图像路径之间的目录,就在图像名称之前,不是吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多