Does SpriteFrameCache support adding async like TextureCache?

Does SpriteFrameCache support adding async like TextureCache?
0.0 0

#1

I need to play an animation when loading resource. I cannot find async function in SpriteFrameCache. I want to use std::thread but I know cocos2d-x is not thread-safe. Can anyone help me?


#2

You can use async load with TextureCache and when it complete, you add the texture loaded to SpriteFrameCache on callback function


#3

You are right! Here is my fix code. Thank u so much!!

for (std::string file_name : {"monsters, items"}) {
  Director::getInstance()->getTextureCache()->addImageAsync(file_name + ".pvr.ccz", [file_name](Texture2D* loaded_texture) {
    SpriteFrameCache::getInstance()->addSpriteFramesWithFile(file_name + ".plist", loaded_texture);
  });
}

#4

Thanks @zhujiawei6000, it works fine, but to unload resource, i’m using this code:

auto textureCache= Director::getInstance()->getTextureCache();
auto spriteFrameCache =SpriteFrameCache::getInstance();

 for (int i=1; i<size +1; i++) {

    spriteFrameCache->removeSpriteFramesFromFile(file_name+std::to_string(i)+".plist");
     
     textureCache->removeTextureForKey(file_name+std::to_string(i)+ ".pvr.ccz");
 }

But, the memory usage is not decrease after runnig the previous code.
Please help me.


#5

let me ask so i can get it clear “monsters” and “items” are individual .plist and .png spritesheets right?