0

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.

Duck
  • 34,902
  • 47
  • 248
  • 470
  • what do you mean "currentScene is a class itself"? – Bryan Chen Mar 02 '12 at 23:57
  • Not class, sorry, I mean, a subclass of cclayer. I have rephrased it. – Duck Mar 03 '12 at 02:01
  • 1
    so u mean that, u use the menu, set up configs for the next game (the menu buttons fire on a different delegate I suppose) and u need the tell that layer to reload a new game right? Why dont u create a static singleton on the CCLayer class the game is running on, create a static method on the class for the new game "+(void)startNewGame" where that static method, passes the command to the static singleton that was set in its init? it may not be the best way, but it should get it done if thats what you are looking for. – Ehab Amer Mar 03 '12 at 02:47
  • what do you mean? to create the main scene (the one which called the menu) as a singleton? (yes, the menu object class is the own menu delegate). – Duck Mar 06 '12 at 13:31

2 Answers2

1

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.

Community
  • 1
  • 1
Mazyod
  • 22,319
  • 10
  • 92
  • 157
0

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.

Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184