在cocos2d- x3中异步预加载精灵表并使用它

Preload spritsheet asynchronously in cocos2d-x 3 and use it

本文关键字:精灵 加载 x3 cocos2d- 异步      更新时间:2023-10-16

这是正确的预加载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的内存中,但还没有关于精灵表中图像位置的信息。因此,我们也调用SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ingame.plist", texture);来要求cocos2d-x解析.plist文件并了解精灵表中的图像在哪里。这有意义吗?

如上所述,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");
}