Whats the best way in Cocos v3 R0 to cache sprites

Hi

After version v3 r0, or all the version 3 and later, a lot of best practices have changed. So i have some questions:

1 - Whats the correct way and best way to load sprite sheet and create animation with tiles, since sprite batch node was deprecated?

2 - In my game i have sprites, layers and PhysicBodies. I have a physic world and touch listener too. When i need make a transition to game over or game win scene, what objects i need remove from memory or what i need do to unload and free all resources from in-game scene?

3 - I need in my game load all sprites to memory to dont need load the same sprites/images every time that i create an enemy or coin. How i can cache the images to dont need reload from disk?

I think that it will solve a lot of problems from community and not only mine. Like my post when i teach about new physic contact listener, it will be a reference for everyone.

Thanks for any help about it and some sample.

Can anyone help us with it?

  1. In v3.x, engine can do auto-batching, so you don’t have to do it with SpriteBatchNode.
  2. Engine will do it for you. But all textures are cached as previous versions. If you don’t need them any more, you can use TextureCache::removeAllTextures() to remove them.
  3. You don’t have to do anything, engine caches textures automatically. The mechanism hasn’t changed since the first version of cocos2d-x.

But there is any way to load all images before use in sprites? Im thinking im make a “loading screen” that will load all textures instead of load when create the enemy or coin, i think that it is better, understand? There is anyway to do it instead of create a lot of sprites?

If i call on loading screen:

Director::getInstance()->getTextureCache()->addImage("sprite1.png");
Director::getInstance()->getTextureCache()->addImage("sprite2.png");
Director::getInstance()->getTextureCache()->addImage("sprite3.png");
...

After load the images for this scene (Stage1Scene for example) i want unload the textures, i never will need it if the player dont start that stage again.

How i can do it?

Reading the source code, i can use:

removeTextureForKey

And use this method to remove Textures using it filename, example:

Director::getInstance()->getTextureCache()->removeTextureForKey("sprite1.png");
Director::getInstance()->getTextureCache()->removeTextureForKey("sprite2.png");
Director::getInstance()->getTextureCache()->removeTextureForKey("sprite3.png");
...

This is OK?

In order to load texture without creating Sprite, you can use TextureCache::addImage() or Texture::addImageAsync. And you can use TextureCache::removeTexture() or TextureCache::removeTextureForKey() to remove a texture.

@zhangxm How to use the newest feature you said ‘In v3.x, engine can do auto-batching, so you don’t have to do it with SpriteBatchNode.’
I found all examples in Cocos v3 only support the old cache way that addChild in batchNode:

`SpriteBatchNode1::SpriteBatchNode1()
{
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesEnded = CC_CALLBACK_2(SpriteBatchNode1::onTouchesEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

auto BatchNode = SpriteBatchNode::create("Images/grossini_dance_atlas.png", 50);
addChild(BatchNode, 0, kTagSpriteBatchNode);

auto s = Director::getInstance()->getWinSize();
addNewSpriteWithCoords( Point(s.width/2, s.height/2) );

}

void SpriteBatchNode1::addNewSpriteWithCoords(Point p)
{
auto BatchNode = static_cast<SpriteBatchNode*>( getChildByTag(kTagSpriteBatchNode) );

int idx = CCRANDOM_0_1() * 1400 / 100;
int x = (idx%5) * 85;
int y = (idx/5) * 121;


auto sprite = Sprite::createWithTexture(BatchNode->getTexture(), Rect(x,y,85,121));
BatchNode->addChild(sprite);

...

}`

If I want to use newest feature, should I change to:
`SpriteBatchNode1::SpriteBatchNode1()
{
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesEnded = CC_CALLBACK_2(SpriteBatchNode1::onTouchesEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

auto BatchNode = SpriteBatchNode::create("Images/grossini_dance_atlas.png", 50);
addChild(BatchNode, 0, kTagSpriteBatchNode);

auto s = Director::getInstance()->getWinSize();
addNewSpriteWithCoords( Point(s.width/2, s.height/2) );

}

void SpriteBatchNode1::addNewSpriteWithCoords(Point p)
{
//auto BatchNode = static_cast<SpriteBatchNode*>( getChildByTag(kTagSpriteBatchNode) );

int idx = CCRANDOM_0_1() * 1400 / 100;
int x = (idx%5) * 85;
int y = (idx/5) * 121;


auto sprite = Sprite::createWithTexture(BatchNode->getTexture(), Rect(x,y,85,121));
this->addChild(sprite);

...

}`