在cocos2d-x 3中异步预加载spritsheet并使用它

时间:2014-07-09 12:42:30

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

这是正确的异步预加载spritsheet并在删除加载屏幕后在其他场景中正确使用它的原因吗?

   // load the texture into the cache 
   Director::getInstance()->getTextureCache()->addImageAsync("ingame.png", 
    [](cocos2d::Texture2D *texture) {

        SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ingame.plist", texture);

        // here remove loading screen and go to ingame scene to use the loaded spritesheet
    }
);


    // use the loaded spritesheet in ingame scene
    auto bg = Sprite::createWithSpriteFrameName("ingame_bg.png");
    bg->setPosition(origin + visibleSize / 2);
    addChild(bg, 0);

据我所知,在调用addImageAsync后,.png文件被加载到GPU的内存中,但是没有关于spritesheet中图像位置的信息。因此,我们还调用SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ingame.plist", texture);来请求cocos2d-x解析.plist文件,并了解spritesheet中图像的位置。是否有意义?

1 个答案:

答案 0 :(得分:1)

如上所述,addSpriteFramesWithFile()会为您预加载。

检查来源:

Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(texturePath.c_str());

if (texture)
{
    addSpriteFramesWithDictionary(dict, texture);
    _loadedFileNames->insert(pszPlist);
}
else
{
    CCLOG("cocos2d: SpriteFrameCache: Couldn't load texture");
}