How to use virtual methods from base class to subclass?

How to use virtual methods from base class to subclass?
0.0 0

#1

Hi,

I want to create base scene manager because i don’t want to copy/paste lots of code. I renamed helloworld to basescene. I added to virtual myInit function. I create new class inherited from basescene. I override to myInit in new class. I call lastline on init function in basescene, but not calling in subclass. Called in baseclass. How can i called?

Thanks.


#2

Show your code, please.


#3

Thanks for reply.

BaseScene.h

#ifndef __BASE_SCENE_H__
#define __BASE_SCENE_H__

#include "cocos2d.h"

class BaseScene : public cocos2d::Scene
{
public:
    static cocos2d::Scene* createScene();
    
    virtual bool init();
    virtual bool myInit() { return true; };
    
    // a selector callback
    void menuCloseCallback(cocos2d::Ref* pSender);
    
    // implement the "static create()" method manually
    CREATE_FUNC(BaseScene);
};

#endif // __BASE_SCENE_H__

BaseScene.cpp

#include "BaseScene.h"
#include "SimpleAudioEngine.h"

USING_NS_CC;

Scene* BaseScene::createScene()
{
    return BaseScene::create();
}

// on "init" you need to initialize your instance
bool BaseScene::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Scene::init() )
    {
        return false;
    }
    
    auto visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    
    /////////////////////////////
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.
    
    // add a "close" icon to exit the progress. it's an autorelease object
    auto closeItem = MenuItemImage::create(
                                           "CloseNormal.png",
                                           "CloseSelected.png",
                                           CC_CALLBACK_1(BaseScene::menuCloseCallback, this));
    
    float x = origin.x + visibleSize.width - closeItem->getContentSize().width/2;
    float y = origin.y + closeItem->getContentSize().height/2;
    closeItem->setPosition(Vec2(300,300));
    
    // create menu, it's an autorelease object
    auto menu = Menu::create(closeItem, NULL);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu);
    
    return myInit();
}


void BaseScene::menuCloseCallback(Ref* pSender)
{
    //Close the cocos2d-x game scene and quit the application
    Director::getInstance()->end();
    
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
    
    /*To navigate back to native iOS screen(if present) without quitting the application  ,do not use Director::getInstance()->end() and exit(0) as given above,instead trigger a custom event created in RootViewController.mm as below*/
    
    //EventCustom customEndEvent("game_scene_close_event");
    //_eventDispatcher->dispatchEvent(&customEndEvent);
}

MenuScene.h

#ifndef __MENU_SCENE_H__
#define __MENU_SCENE_H__

#include "cocos2d.h"
#include "BaseScene.h"

class MenuScene : public BaseScene
{
public:
    virtual bool myInit() override;
};

#endif // __MENU_SCENE_H__

MenuScene.cpp

#include "MenuScene.h"

USING_NS_CC;

// on "sceneInit" you need to initialize your instance
bool MenuScene::myInit()
{
    
    return true;
}