Replacing scenes when using a UITabBar - cocos2d-iphone

My application has four tabBarItems. Two of the tabBarItems use CCScene (Cocos2d) classes. I start on one scene which works well, and then I switch over to the second scene using the tabBar which also works well. My problem is that when I try to return to the original scene, my view is the first scene but the actual running scene hasn't changed from the second scene.
Is there a way to switch scenes when toggling on a tabBar?
Should each tabBarItem simply link to a CCLayer, and then have all those layers become children to a single CCScene object?
Any suggestions will be appreciated.

Related

Cocos2d: the best way to compose an object that will contain several Sprites

I need to create an object that contains several sprites (2 for simplicity: the game object and its' shadow).
I need to compose them into the same class, so having the following questions:
what is the best ancestor class to the described one?
I used CCNode for this purpose. I overridden its draw method in the following way:
- (void)draw {
[super draw];
[_item draw];
[_itemShadow draw];
}
and suddenly found that need to change all other CCNode methods in the same way. For ex. change position, visible, etc. in order to change these properties in both Sprites my custom container aggregates:
#interface NBChecker : CCNode {
CCSprite *_item;
CCSprite *_itemShadow;
}
#end
There is the other way I see - to make both sprites parent property pointing to self. This should synchronise positions, visibility, etc. for these sprites.
Who has better ideas?
Would like to use aggregation and not sure I'm right in my thoughts.
Thanks!
Scrap the draw code. All you need to do is to add the two sprites as children of the CCNode subclass. Write your logic code in the CCNode subclass and have two ivars for each sprite for easier access (like you already have).
You don't need to draw the sprites yourself as long as they are part of the scene graph.
The sprites will move and rotate relative to their parent automatically. Move the node to move the sprites in unison, etc.

Why am I not able to Add new Layer to Scene from another Layer by parent?

I don't get why this code is incorrect...
DeadPanelLayer* deadPanelLayer = [DeadPanelLayer node];
[(CCScene*)self.parent addChild:deadPanelLayer z:2];
DeadPanel is a layer that I want to add on the Scene running from another layer
any idea why my layer is not loaded?
Edit: the game is not crashing, but the new layer does't show up. I tried to even move the objects on the scene and layer just to make sure it is not displaying due zindex hierarchic
but still...
I tried to add a method on Scene to add the layer in case it is called from parent:
[(GameScene*)self.parent showDeadPanel];
and even get current scene from Director
[[CCDirector sharedDirector] runningScene]
and it doesn't work either

Understanding cocos2d scenes, how they works really?

I'm stuck on something about the scenes creation and replacement in cocos2d, so I'm going to ask precisely what seems to be misunderstood by me. I have a game (fully working except for scene swapping, sadly) with some little-games, now, if I had to do this starting with a cocos2d scene as menu I wouldn't have any problem, but since I did it starting with UIKit I truly need to know better how the scenes are working to fix it.
Firstly, is it required to start a scene in the appDelegate? since I'm starting with UIkit and the scene must be shown after you choice the game (say, out of 3 choices), which scene should I put in the appDelegate? and where exactly? I'm putting the scene in this method:
-(void) directorDidReshapeProjection:(CCDirector*)director
{
if(director.runningScene == nil)
//start scene
}
If I put the FIRST scene, the UIKit part works good and when I start the "game number TWO" as first choice (say we play this game for first) I got the Open GL 0x0506 error, then the scene start.
If I put the first scene, I choice the first game, and then quit and choice the second game, the scene is replaced properly without that error.
If I put the first scene, and I start the "game number 1" it works (obviously) because he has the scene loaded, but I cannot know which game will start as first the user.
I tried with an "intro scene" loaded at the appDelegate but I got the same problem. the problem basically is "how to start scene if you have more than one scene and don't know which will be called as first"...
The 'getting started with iOS' documentation will really clear up a lot of these questions. You can find it at developer.apple.com - https://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007072
It explains just what an AppDelegate actually is, as well as how to use one properly. It is not immediately clear how to mix UIKit and cocos2d, but the above link cleared a lot up for me. Another very helpful resource is a tutorial by Ray Wenderlich -
http://www.raywenderlich.com/4817/how-to-integrate-cocos2d-and-uikit
From a bird's eye view, the CCDirector inherits from a UIWindow. Mixing UIKit and cocos2d is as simple as building your interface with UIKit, then at some point opening a UIWindow and allowing the CCDirector to start cocos2d. In a sense, the components act as almost two entirely separate entities.

Does replaceScene replace only the current scene or does it replace all scenes in Cocos2d

I have a game in Cocos2d with a main scene (game scene) and a button to go to a "Configuration" scene. When the user clicks on the Configuration button in the main scene I use pushScene to go tho the "Configuration" scene. The reason I use pushScene is to allow the user to resume the game where he was left off.
In the "Configuration" scene there are two options: "Cancel" and "Ok". If the user hits "Cancel" I use popScene and the game resumes where it was left of. If the user hits "Ok" I use replaceScene because I want the game to start from the beginning with the new configuration.
So, when the user hits "Ok" I know that the "Configuration" scene is replaced by the new game scene, but does the old game scene gets replaced too? Otherwise, am I doing things correctly or should I implement another way to let the game scene know whether it should resume or restart.
I want to make sure I am not leaking memory by accumulating unreplaced scenes.
The replaceScene method does what it says. It replaces the current scene. If you have 10 scenes pushed onto one another, it will replace the 10th scene and all previous scenes remain.
It's one of the reasons why I don't recommend using pushScene. It's too easy to forget a situation where scenes might get pushed more than they get popped. The other reason is that popScene can't be animated with a transition.
Btw, you can easily test this behavior if you do replaceScene after pushScene, then popScene in the newly replaced scene. You'll see the old scene popping up. Normally if you popScene with just a single scene in the stack it'll throw an assertion.

Can a CCSprite switch between multiple animations?

Is it possible to make a CCSprite switch between two different animations? (both are CCRepeatForever)
I try to use stopAction: and runAction: but it crashes the app.
I can only use pauseSchedulerAndActions: and resumeSchedulerAndActions: with one animation.
To answer your question: Yes. If switching animations troubles you, use one sprite for each animation and keep them all updated to the same position, and only set the one to visible whose animation should be played.
The crash is certainly unrelated.