same CCMenu Adding to Multiple Scenes

144 Views Asked by At

Newbie to iOS and Cocos2d ( 2.x )

Ok I want to create a Menu Object and call it from each new Scene.

Example:

  • Scene1, add menu
  • Scene2, add same menu as on Scene1

I've only seen how to initialize the CCMenu when you init the Layer itself. you build the items and then add them to the CCMenu and so on.

How can I initialize the CCMenu once and then just add it to what ever scene I happen to be viewing? So if I'm viewing Scene1 or Scene2 it's still the same menu.

Does this make sense?

1

There are 1 best solutions below

4
On

You'll need a different instance of the menu for each scene, so technically speaking, you'll need to initialize it once per scene.

But I think you're asking "how can I write the code once and then reuse that code in each scene." You'll want to create some sort of CC Object that you can reuse. This could be a subclass of a CCMenu, CCLayer, or whatever suits the purpose best. So you may try something like:

@interface MyMenuLayer : CCLayer {
  CCMenu *myMenu;
}
@end

Then in the .m file, set up your menu however you like. When you want to include this in Scene1:

MyMenuLayer *menu = [MyMenuLayer node];
[self addChild:menu];

You can use the exact same code in Scene2.

(You could just extend CCMenu instead of CCLayer, but I personally prefer to work with Layers instead of Menus. It's a matter of personal choice.)

It's hard to give a very definitive answer with the information in your question, but I hope this gets you set off on the right path.