So basically what I am trying to do is when the player of my game completes a level (for example level 1), it switches scenes back to the level select scene, and swaps the sprite picture of level 1 to a different one (for example one that has a check mark over it). I can replace the scene but I don't know how to change the sprite in the new scene, specifically when the scene change occurs after the level is completed. So I am assuming I would use a singleton class, am I right? If so, how would I go about using it?
-
no, you wouldn't use a singleton, you'd test to see if you finished the level with Cocos2d's persistent info help stuff. – Stephen J Nov 27 '13 at 20:30
-
have you tried anything ? if so, show it ! Singletons are not your best friends in many circumstances. With the question you are asking, i doubt you could recognize when they would be/not be appropriate. In this case, i doubt it would be a good strategy. – YvesLeBorg Nov 27 '13 at 21:17
2 Answers
Singletons are ok, don't be afraid to use them. Many components of cocos2d are singletons.
I think what you need is some sort of structure that keeps track of the state of the game. (How many levels are completed/What should be the next level/etc). When your level select scene is loaded it should look up that 'state of the game' object (be it a singleton, plist, etc ) and displays itself accordingly.
I would stay away from passing information directly from one scene to another, this makes reordering them a headache later on.

- 2,303
- 1
- 22
- 30
First, let me make sure I understand the problem correctly.
- You have a scene (A) with a sprite in it.
- You transition to another scene (B) for the game play.
- The game ends and you transition back to scene A.
- When scene A redisplays, you want to change the image displayed by the sprite.
If I've got this right, then regardless of whether singletons are good or bad, you don't need one for this.
If, like me, you've created your sprite using a display frame from the CCSpriteFrameCache, then you can simply change the frame you want the sprite to use when "A" is redisplayed.
Some sample code demonstrating this can be seen in another question:
How to switch the image of a CCSprite
(Certainly, if I've got this right, then feel free to just dupe this)
-
That is correct however I only want to change the sprite under the condition that the level is completed. I also have a back button returning to that same scene so that may be problematic – user2499470 Dec 03 '13 at 01:17
-
That shouldn't be a problem. What I normally do in this scenario is set up a delegate that is called by scene B just before the transition back to A. The call informs A of anything it needs to know so that it can make any changes it needs to. The back button could call one delegate message, and the "completion" event could call another. Alternatively, they could call the same one and pass a state as a parameter. – PKCLsoft Dec 03 '13 at 04:09