1
\$\begingroup\$

I am using Cocos2d-x engine with 3.2 version.

In my splash screen I am prefeching a plist by below line

SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ui.plist");

Now inside game when I am creating a sprite I am getting an assert fail error

Sprite *ss = Sprite::createWithSpriteFrameName("pause.png");

Assert failed: Invalid spriteFrameName: pause.png

Also when the splash screen is removed, I am getting the below line in console

cocos2d: TextureCache: removing unused texture: D:/cocos2dx/projects/ABCD/Resources/mid/ui.png

Is there anything I need to add or something?
I was doing the same thing for 2.x and was working but not in 3.2

EDIT: In 3.x if you are calling this line Director::getInstance()->purgeCachedData();, remember that it will delete all textures from the cache memory. So handle all sprite-sheets manually.

\$\endgroup\$
4
  • \$\begingroup\$ Do your scene change from time you load sprite-sheet to the time you create sprite ? \$\endgroup\$
    – Emadpres
    Commented Nov 29, 2014 at 16:28
  • \$\begingroup\$ my scene change on every menu, so YES. splash->main menu->level select->in-game but same case was working with 2.x.x \$\endgroup\$
    – smit patel
    Commented Nov 29, 2014 at 16:44
  • \$\begingroup\$ I face this unloading problem in my projects. cocos2d unload when facing lack of memory. I solved my problem by calling addSpriteFramesWithFile() in new scenes as solution. I hope some one from cocos2d team answer the reason of this strange unloading. \$\endgroup\$
    – Emadpres
    Commented Nov 29, 2014 at 17:56
  • \$\begingroup\$ Maybe this tutorial would help codeandweb.com/texturepacker/tutorials/… \$\endgroup\$
    – Aji Imawan
    Commented Jan 25, 2020 at 23:06

1 Answer 1

2
\$\begingroup\$

Do you have a SpriteBatchNode object added to the scene? According to the cocos2d-x wiki:

A SpriteBatchNode object contains the actual image texture of all the sprite frames. You must add it to a scene, even though it won’t draw anything itself; it just needs to be there so that it is part of the rendering pipeline. For example:

SpriteBatchNode* spritebatch = SpriteBatchNode::create("animations/grossini.png");

Next, you need to use the SpriteFrameCache singleton to keep track how frame names correspond to frame bounds – that is, what rectangular area of the sprite sheet. Example:

SpriteFrameCache* cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile("animations/grossini.plist");

Once your sprite sheet and frames are loaded, and the sprite sheet has been added to the scene, you can create sprites that use these frames by using the createWithSpriteFrameName method, and adding it as a child of the sprite sheet:

m_pSprite1 = Sprite::createWithSpriteFrameName("grossini_dance_01.png");
spritebatch->addChild(m_pSprite1);
addChild(spritebatch);

If I use "createWithSpriteFrameName("sample.png")" my app crashes. But if I use "initWithSpriteFrameName("sample.png")" the app runs perfectly, I tested this just now before writing this. And I also call addSpriteFramesWithFile from every scene to deal with the unloading.

\$\endgroup\$
5
  • \$\begingroup\$ You mean I need to create a spritebatch in the scene to force cocos not to unload texture ? But what if I want to preload a texture some scene earlier than where I use. I should create a spritebatch and add it to scene fakely in each scene until reaching desire scene ? \$\endgroup\$
    – Emadpres
    Commented Dec 2, 2014 at 11:55
  • \$\begingroup\$ If there is a memory warning, all unused preloaded textures will be unloaded afaik. So I preload the textures in a loading scene, and then in the (lets say) GameScene again call "addSpriteFramesWithFile" so that if the textures were unloaded they get loaded again. If you take a look at the "addSpriteFramesWithFile" method implementation it checks if the .plist is currently loaded or not before doing anything. I'm not sure if this is the best way to do this though, would love to see a better way to handle resource management. \$\endgroup\$
    – war1oc
    Commented Dec 2, 2014 at 14:12
  • \$\begingroup\$ Your answer is not helpful at all and there is no such method "initWithSpriteFrameName" in 3.x, don't know about 2.x \$\endgroup\$
    – smit patel
    Commented Dec 15, 2014 at 7:08
  • \$\begingroup\$ @smit-patel here is the reference link for Cocos2d-x-3.3rc which clearly states all the available methods. I myself have used this method in two games I built using v3.2. \$\endgroup\$
    – war1oc
    Commented Dec 17, 2014 at 18:11
  • \$\begingroup\$ For the record war1oc, this was a helpful answer. Don't know why this dude was being rude. \$\endgroup\$ Commented Apr 26, 2016 at 14:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .