Duck
Duck

Reputation: 35933

Cocos2D iPhone - running a method on the main scene from a CCLayer

I have this main scene in a game and I have called the menu where the user can choose the parameters for the new match (number of players, level of difficulty, etc).

This menu was created on a CClayer and presented on top of the main scene using this:

CCLayer *menu = [Menu node];

id actionFadeIn = [CCFadeIn actionWithDuration:0.3];
[menu runAction:[CCSequence actions:actionFadeIn, nil]];

[self addChild:menu z:1 tag:theMenu];

This menu's class has a basic logic there. For every parameter chosen on that menu a proper parameter is set on a singleton. Now that the user has chosen all parameters it will press the START GAME button.

When this happens, the menu has to vanish and a method called startGame has to run on the main scene, but this is my problem: How do I run the method from the menu class? I thought I could do

CCScene *currentScene = [[CCDirector sharedDirector] runningScene];
[currentScene startGame];

but I cannot do that because the current scene is not an instance itself and each of its methods can only be run from inside scene, but not from the outside.

I thought of using notifications to post an order to run the method on the class, but that sounded lame and like using a cannon to kill a fly. How the best way to do that in Cocos?

thanks.

Upvotes: 0

Views: 917

Answers (2)

Mazyod
Mazyod

Reputation: 22559

I have a very detailed answer to your question at the following post:

Accessing Objects in other Layers (cocos2d)

You basically make your Scene a "semi-singleton". Other ways are also explained in case you prefer a different approach.

Upvotes: 1

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39978

I think vanishing menu is easy you just need to call the vanishing method on the same layer i.e Menu. Now when you press the start button you can call a method in Menu class say startPressed

- (void)startPressed{
   [[self parent] start];
}

The parent of the menu layer is your game layer.

Upvotes: 0

Related Questions