1
\$\begingroup\$

some times it takes time to create the sprite structures in the scene, I might need to setup structures inside this sprite to meet requirement, thus I would hope to reuse such structures with the game again and again.

I tried that, remove the child from parent, detach it from parent , clean parent with the sprite. but when I try to add the sprite to another scene, it's just wont pass the assertion that the sprite already have parent

did I miss some step ?

add an example:

I have a sprite A which involves of quite a few steps to construct, so I used it in scene A layer A, and then I want to use it in scene A layer B, scene B layer A1 etc..... generally speaking I don't want to reconstruct the sprte again.

\$\endgroup\$
1
  • \$\begingroup\$ How did you create your Sprites; CCSprite::create(...) or manually new ? \$\endgroup\$
    – Emadpres
    Jun 22, 2014 at 8:53

2 Answers 2

1
\$\begingroup\$

Do you mean you want to quickly reuse sprite without re-create it?

You may need to consider using ObjectPool to create those sprites ahead of time for certain number of instances you may need, then whenever you need to use it in the game, there will be no time wasted in creation.

Edit: ObjectPool is where it has a whole bunch of objects already created ahead of time. Basically it has 2 managed arrays of objects which are free-array and active-array. At first, you just pre-create those objects to cut the time that would be lost and would hurt user experience if you create them on-the-fly during gameplay, all of those objects sit in free-array. Then whenever you need to use it, you pop it one-by-one from free-array and use in your game. So they will be removed from free-array and added into active-array. After you're done using it, you reset their states and return them back to free-array. It goes like this. Performance is great in magnitude compared to on-the-fly approach.

\$\endgroup\$
2
  • \$\begingroup\$ can you elaborate ? \$\endgroup\$
    – zinking
    May 26, 2013 at 16:08
  • \$\begingroup\$ I edited my answer, please take a look again. \$\endgroup\$
    – haxpor
    May 26, 2013 at 19:14
0
\$\begingroup\$

It's a guess, but are you sure you are retaining the sprite before detaching it from it's parent? when you detach an sprite (or any node in general), it usually get's deleted too.

In cocos2d for every instance, it's counted how many objects, are depending on that instance to work. In other words, the system keeps track of how many times that object is retains. so when you stop using that instance, it's cleaned up automatically. Adding an sprite as a child results in that sprite being retained, and later removing it, results in it being released. In your case it's possible that removing that sprite from parent, which causes a release, brings "retain count" down to zero, and ultimately deletes the sprite. Thus when you later try to attach to another node, you are attaching a deleted pointer. Try this instead:

sprite->retain();
sprite->removeFromParent();
newParent->addChild(sprite);
sprite->release();

note that you also need to release the sprite at the end of the process, so that later that sprite could be deleted normally.

\$\endgroup\$

You must log in to answer this question.

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