extending a cclayer

if I have a …

class MenuShell : public CCLayer

and I want to extend MenuShell like

class MainMenu :public MenuShell

or

class SettingsMenu :public MenuShell

how?

I can’t figure out the LayerTest examples

Help Please!

Regards H

Are you remembering to include the class you’re trying to extend in your header file? So, #include “MenuShell.h”
If you are, then it would be helpful to see some code :slight_smile:

Thanks for the interest Tim

What I have now …

MenuShell.h….

#ifndef ProjectMenuShell*_
#defineProjectMenuShell*_

#include “cocos2d.h”

USING_NS_CC;

class MenuShell : public CCLayer
{

public:

virtual bool init();

static CCScene* scene();

virtual void onEnter();

void menuCloseCallback(CCObject* pSender);

CREATE_FUNC(MenuShell);

};

class MainMenu : public MenuShell
{

public:

virtual void onEnter();

};

#endif /* defined(ProjectMenuShell__) */

and MenuShell.cpp….

#include “MenuShell.h”

using namespace cocos2d;

bool MenuShell::init()
{

if ( !CCLayer::init() )
{
return false;
}

CCLOG (“In Menu Shell”);

return true;
}

void MenuShell::onEnter(){

}

void MenuShell::menuCloseCallback(CCObject* pSender)
{
CCDirector::sharedDirector()>end;
#if
exit;
#endif
}
CCScene* MenuShell::scene
{
// ‘scene’ is an autorelease object
CCScene scene = CCScene::create;
// ‘layer’ is an autorelease object
MenuShell
layer = MenuShell::create;
// add layer as a child to scene
scene
>addChild(layer);

// return the scene
return scene;
}

void MainMenu::onEnter(){

MenuShell::onEnter();

CCLOG (“In Main Menu”);

}
I’m calling MainMenu from another scene by …

CCDirector::sharedDirector()->replaceScene(MainMenu::scene());

Which is only setting up the static scene for MenuShell

i’m coming from an objective C background so I’m a bit green on the how things work in C++

H

  1. Try using pre and /pre tags on your code when posting in the forums.
  2. It seems that you are not familiar with inheritance in C++. Might be easier if you study that for a bit first because it will help GREATLY when you learn it.
  3. If you can explain what you’re trying to do, we can help you even better.

In all fairness Lance it can’t be all that different to any other OOP language. I understand the concept of inheritance. If it was a straight forward object it wouldn’t be a problem but because a CCLayer needs to have a scene to hold it, I appreciate its a bit tricky. I can do it in Javascript or Obj C … surely someone can put down a bit of C++ code that simply shows how to take a CCScene scene add layer2 that extends a CCLayer layer1.??

I don’t really get what you’re trying to do. Do you want to create a MenuShell object on the scene or a MainMenu object on the scene? Please explain what you want to achieve.

You said you want to create a scene with layer2 (in this case, MenuShell) which extends from layer1 (MainMenu).
Then simply create a scene with MenuShell in it and show that scene.

// MenuShell.h
#include "MainMenu.h"

class MenuShell : public MainMenu {
private:

public:
   static cocos2d::CCScene * scene();
   CREATE_FUNC( MenuShell );
}

// MenuShell.cpp
#include "MenuShell.h"

using namespace cocos2d;

CCScene * MenuShell::scene() {
   CCScene * scene = CCScene::create();
   MenuShell * layer = MenuShell::create();
   scene -> addChild( layer );

   return scene;
}

// Somewhere
bool HelloWorld::changeScene() {
   CCDirector::sharedDirector() -> replaceScene( MenuShell::scene() );
}

Unless you mean to have a scene with MainMenu AND MenuShell in it?

Lance that’s lovely … exactly what I wanted … and so easy in the end.

The menu example probably isn’t great but also I use it to implement game levels where the game engine layer has the main body of the game and the game levels use bits and pieces of it as required

class Level1: public GameEngine

class Level2: public GameEngine

Maybe its of no use to anyone else but I’m pleased

Thanks Harry