Implementation of the replacing game scene in C++ - c++

I have about 10 scene classes in my C++ game. It's something like a regular game screen (menu screen, game screen, leaderboard, options, etc). So I need some technique to replace one scene to another. I've read all comments you posted me in this thread and tried to implement it. Now I have following code:
// -----[ main.cpp ]----- //
#include "SceneManager.h"
#include "Scene.h"
int main( int argc, const char * argv[] ) {
SceneManager *sceneManager = new SceneManager();
sceneManager->changeScene( 0 );
delete sceneManager;
return 0;
}
// -----[ Scene.h ]----- //
class SceneManager;
class Scene {
public:
SceneManager *sceneManager;
void start( SceneManager *sceneManager );
};
// -----[ SceneManager.h ]----- //
#include "Scene.h"
#include "MenuScene.h"
#include "GameScene.h"
class Scene;
class MenuScene;
class GameScene;
class SceneManager {
public:
Scene* scene;
void changeScene( short id ) {
if ( scene != NULL ) {
delete scene; // to prevent a memory leak
}
if ( id == 0 ) {
scene = new MenuScene();
} else if ( id == 1 ) {
scene = new GameScene();
}
if (scene) {
scene->start( this );
}
}
};
// -----[ MenuScene.h ]----- //
class MenuScene: public Scene {
public:
void start( SceneManager *sceneManager ) {
this->sceneManager = sceneManager;
}
};
// -----[ GameScene.h ]----- //
class GameScene: public Scene {
public:
void start( SceneManager *sceneManager ) {
this->sceneManager = sceneManager;
}
};
It doesn't work because of error (XCode 4.6, MacOS X):
Apple Match-O Linker (id) Error
Undefined symbols for architecture x86_64:
"Scene::start(SceneManager*)", referenced from:
SceneManager::changeScene(short) in main.o
ld: symbol(s) not found for architecture x86_64
What do i do wrong? How to fix it? Maybe someone knows about some popular issues on this subject?

As mentioned in the comments, I believe your design conceptually needs another class. Here is a rough example.
class Scene {
public:
virtual void start() = 0;
};
class SceneManager {
public:
void changeScene( short id ) {
Scene* scene = NULL;
if ( id == 0 ) {
scene = new MenuScene(); // undeclared yet
} else if ( id == 1 ) {
scene = new GameScene(); // undeclared yet
}
if (scene) {
// TODO: make the scene visible.
scene->start();
}
}
};
class MenuScene: public Scene {
public:
virtual void start() {
// Draw menu stuff
}
};
class GameScene: public Scene {
public:
void start() {
// Draw game stuff
}
};
It's hard to be more specific than that without knowing more context about your particular environment.

Related

Keep receiving error: expected primary-expression before ' , ' token

I'm kinda new to C++ however, this was a code that was provided to me and I was told to do some edits but very few. I Keep getting the error for two lines of code:
MyFrame() : wxFrame((wxFrame *)NULL, -1, wxT("wxTimerDemo " + wxDataTime::Now().Format("%c"))
, wxPoint(50,50), wxSize(500,300))
and
dc.DrawText(wxT("Testing"), 40, y);
The top one is the only code edited which was followed exactly as it should've.
Full code:
// Render timer - use of a timer
// http://wiki.wxwidgets.org/Making_a_render_loop
#include <wx/sizer.h>
#include <wx/wx.h>
#include <wx/timer.h>
// prototypes
class BasicDrawPane;
class MyFrame;
// class definitions
class RenderTimer : public wxTimer
{
BasicDrawPane* pane;
public:
RenderTimer(BasicDrawPane* pane);
void Notify();
void start();
};
#define wxT(x)
class BasicDrawPane : public wxPanel
{
public:
BasicDrawPane(wxFrame* parent);
void paintEvent(wxPaintEvent& evt);
void render( wxDC& dc );
DECLARE_EVENT_TABLE()
};
class MyApp: public wxApp
{
bool OnInit();
MyFrame* frame;
public:
};
RenderTimer::RenderTimer(BasicDrawPane* pane) : wxTimer()
{
RenderTimer::pane = pane;
}
void RenderTimer::Notify()
{
pane->Refresh();
}
void RenderTimer::start()
{
wxTimer::Start(10);
}
IMPLEMENT_APP(MyApp)
class MyFrame : public wxFrame
{
RenderTimer* timer;
BasicDrawPane* drawPane;
public:
MyFrame() : wxFrame((wxFrame *)NULL, -1, wxT("wxTimerDemo " + wxDataTime::Now().Format("%c"))
, wxPoint(50,50), wxSize(500,300))
{
drawPane = new BasicDrawPane( this );
timer = new RenderTimer(drawPane);
Show();
timer->start();
}
~MyFrame()
{
delete timer;
}
void onClose(wxCloseEvent& evt)
{
timer->Stop();
evt.Skip();
}
DECLARE_EVENT_TABLE()
};
// event table for MyFrame
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_CLOSE(MyFrame::onClose)
END_EVENT_TABLE()
bool MyApp::OnInit()
{
frame = new MyFrame();
frame->Show();
return true;
}
// event table for BasicDrawPane
BEGIN_EVENT_TABLE(BasicDrawPane, wxPanel)
EVT_PAINT(BasicDrawPane::paintEvent)
END_EVENT_TABLE()
BasicDrawPane::BasicDrawPane(wxFrame* parent) :
wxPanel(parent)
{
}
void BasicDrawPane::paintEvent(wxPaintEvent& evt)
{
wxPaintDC dc(this);
render(dc);
}
void BasicDrawPane::render( wxDC& dc )
{
static int y = 0;
static int y_speed = 2;
y += y_speed;
if(y<0) y_speed = 2;
if(y>200) y_speed = -2;
dc.SetBackground( *wxWHITE_BRUSH );
dc.Clear();
dc.DrawText(wxT("Testing"), 40, y);
}
Thanks for any help.
#define wxT(x) transforms any x to blank.
dc.DrawText(wxT("Testing"), 40, y); becomes dc.DrawText( , 40, y); and now the error is obvious.
You should define the macro like
#define wxT(x) x
or
#define wxT(x) (x)
Defining the macro wxT is not a good use case, it should be defined when you #include <wx/string.h>, thus #define wxT(x) should be removed from your code. For more information visit #define wxT ( string ). Note that since wxWidgets 2.9.0 you shouldn't use wxT() anymore in your program sources (it was previously required if you wanted to support Unicode).

SFML sf::Mouse::getPosition method cannot write the correct argument

My problem is that I have written this code inside Game::HandleInput() method but I cannot make the sf::Mouse::getPosition() method to get the mouse coordinates relative to window. Without argument, I don't get an error. However, ship doesn't rotate properly. I have tried getPosition(m_window) and getPosition(&m_window). I am getting this error:
no instance of overloaded function "sf::Mouse::getPosition" matches the argument list
EDIT: UPDATED THE WINDOW.H
Window.h:
class Window{
//Constructers
public:
Window();
Window(const std::string& l_title, const sf::Vector2u& l_size);
...
private:
sf::RenderWindow m_window;
...
}
EDIT: ADDED THE FULL CODE OF WINDOW.CPP:
Window.cpp:
#include "Window.h"
Window::Window() {
Setup("Window", sf::Vector2u(640, 480));
}
Window::Window(const std::string& l_title, const sf::Vector2u& l_size) {
Setup(l_title, l_size);
}
Window::~Window() {
Destroy();
}
void Window::Setup(const std::string& l_title,
const sf::Vector2u& l_size)
{
m_windowTitle = l_title;
m_windowSize = l_size;
m_isFullscreen = false;
m_isDone = false;
Create();
}
void Window::Create() {
auto style = (m_isFullscreen ? sf::Style::Fullscreen
: sf::Style::Default);
m_window.create({ m_windowSize.x, m_windowSize.y, 32 },
m_windowTitle, style);
}
void Window::Destroy() {
m_window.close();
}
void Window::Update() {
sf::Event event;
while (m_window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
m_isDone = true;
}
else if (event.type == sf::Event::KeyPressed &&
event.key.code == sf::Keyboard::F5)
{
ToggleFullscreen();
}
}
}
void Window::ToggleFullscreen() {
m_isFullscreen = !m_isFullscreen;
Destroy();
Create();
}
void Window::BeginDraw() { m_window.clear(sf::Color::Black); }
void Window::EndDraw() { m_window.display(); }
bool Window::IsDone() { return m_isDone; }
bool Window::IsFullscreen() { return m_isFullscreen; }
sf::Vector2u Window::GetWindowSize() { return m_windowSize; }
void Window::Draw(sf::Drawable& l_drawable){
m_window.draw(l_drawable);
}
EDIT: UPDATED THE GAME.H
Game.h:
class Game{
public:
Game();
~Game();
void HandleInput();
void Update();
void Render();
Window* GetWindow();
private:
...
Window m_window;
...
}
EDIT: UPDATED THE GAME.CPP
Game.cpp:
Game::Game() : m_window("Multiplayer Space Shooter Game", sf::Vector2u(800, 600)) {
// Setting up class members.
m_shipText.loadFromFile("C:\\Users\\AliTeo\\Desktop\\Piksel çalışmaları\\ship_pixel2.png");
m_ship.setTexture(m_shipText);
m_ship.setOrigin(m_shipText.getSize().x / 2, m_shipText.getSize().y / 2);
m_ship.setPosition(320, 240);
}
void Game::HandleInput() {
...
//Get the angle between ship and mouse.
//Error if there is an argument in getPosition()
m_angle = atan2(sf::Mouse::getPosition().y - m_ship.getPosition().y, sf::Mouse::getPosition().x - m_ship.getPosition().x); //TO DO: getPosition(&Relative To)
m_angle *= 180 / m_PI;
...
}
Window* Game::GetWindow() { return &m_window; }
EDIT: ADDED THE MAIN.CPP
Main.cpp
int main() {
Game game;
while (!game.GetWindow()->IsDone()) {
game.HandleInput();
game.Update();
game.Render();
}
}
First let me give you what I assume is a minimal example reproducing your problem:
#include <SFML/Graphics.hpp>
class MyWindow {
public:
MyWindow()
: m_window({800, 600, 32}, "my window title") {}
private:
sf::RenderWindow m_window;
};
int main() {
MyWindow window;
sf::Mouse::getPosition(window);
}
This code doesn't compile (and admittedly wouldn't do anything interesting when compiled, but that's not the point). I suspect it'd give you the same error that you're currently having if you tried to compile it.
Please note that this is what we expect when we talk about a MCVE: this code is short, simple, exhibits the error and would compile if not because of it.
Besides, it makes the error painfully clear, and if you tried to come up with a MCVE yourself, you may have solved your problem without having to post a question here, which would certainly save you time.
Contrast with your code:
m_angle = atan2(sf::Mouse::getPosition().y - m_ship.getPosition().y
,sf::Mouse::getPosition().x - m_ship.getPosition().x
);
//TO DO: getPosition(Relative To)
This code is legal, but you explained that it is incorrect and you wanted to turn it into something along those lines:
m_angle = atan2(sf::Mouse::getPosition(m_window).y - m_ship.getPosition().y
,sf::Mouse::getPosition(m_window).x - m_ship.getPosition().x
);
//TO DO: getPosition(Relative To)
... which doesn't compile.
However, in this scope m_window is a Window not a sf::RenderWindow!
The problem is that you're passing a reference to an object (MyWindow in my example, Window in your case) that encapsulates a sf::RenderWindow, but isn't convertible to sf::Window& itself.
Therefore, you can't pass it to sf::Mouse::getPosition which expects either nothing or a sf::Window&, but certainly not a Window& or a MyWindow&.
There are a lot of ways of fixing this. Two of which are presented below:
#include <SFML/Graphics.hpp>
class MyWindow {
public:
MyWindow()
: m_window({800, 600, 32}, "my window title") {}
// you could add an accessor
const sf::Window& getSfmlWindow() const { return m_window; }
// you may also expose a method to get the mouse position
// relatively to this window
const sf::Vector2i getMousePosition() const {
return sf::Mouse::getPosition(m_window);
}
private:
sf::RenderWindow m_window;
};
int main() {
MyWindow window;
sf::Vector2i mouse_position;
// this won't work! window isn't convertible to sf::Window&
// mouse_position = sf::Mouse::getPosition(window);
// using the accessor
mouse_position = sf::Mouse::getPosition(window.getSfmlWindow());
// or the exposed method
mouse_position = window.getMousePosition();
}

cocos2d-x beta3 LayerColor doesn't change background color

can't change the color of a background i have this simple class :
here is the c++ file :
#include "HelloWorldScene.h"
USING_NS_CC;
HelloWorld::HelloWorld()
{
;
}
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !LayerColor::initWithColor(Color4B(20,0,0,255)) )
{
return false;
}
winSize = Director::getInstance()->getWinSize();
visibleSize = Director::getInstance()->getVisibleSize();
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(HelloWorld::menuCloseCallback, this));
closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
origin.y + closeItem->getContentSize().height/2));
// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Point::ZERO);
this->addChild(menu, 1);
schedule( schedule_selector(HelloWorld::tick) );
return true;
}
void HelloWorld::onExit()
{
LayerColor::onExit();
}
void HelloWorld::onEnter()
{
LayerColor::onEnter();
auto cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile("interface/sprites.plist", "interface/sprites.png");
SpriteBatchNode* batch = SpriteBatchNode::create("interface/sprites.png");
this->addChild(batch,BATCH_Z);
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan, this);
listener->onTouchesMoved = CC_CALLBACK_2(HelloWorld::onTouchesMoved, this);
listener->onTouchesEnded = CC_CALLBACK_2(HelloWorld::onTouchesEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}
void HelloWorld::onTouchesBegan(const std::vector<Touch*>& touches, Event *event)
{
for( auto& touch : touches)
{
}
}
void HelloWorld::onTouchesMoved(const std::vector<Touch*>& touches, Event *event)
{
for( auto& touch : touches)
{
}
}
void HelloWorld::onTouchesEnded(const std::vector<Touch*>& touches, Event *event)
{
for( auto& touch : touches)
{
startAnim = true;
};
}
void HelloWorld::tick(float dt)
{
if(startAnim)
{
}
}
void HelloWorld::draw()
{
LayerColor::draw();
}
HelloWorld::~HelloWorld()
{
// Removes Touch Event Listener
_eventDispatcher->removeEventListener(_touchListener);
}
void HelloWorld::menuCloseCallback(Object* pSender)
{
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
and the h file :
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class GameObj;
class ReelGameObj;
class HelloWorld : public cocos2d::LayerColor
{
public:
HelloWorld();
~HelloWorld();
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// a selector callback
void menuCloseCallback(Object* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
void tick(float dt);
virtual void draw();
virtual void onEnter();
virtual void onExit();
void onTouchesBegan(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *event);
void onTouchesMoved(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *event);
void onTouchesEnded(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *event);
protected:
cocos2d::CustomCommand _customCommand;
void onDraw();
private:
cocos2d::EventListenerTouchOneByOne* _touchListener;
cocos2d::Size winSize;
cocos2d::Size visibleSize;
cocos2d::Point origin;
GameObj* pMainWindowObjCenter;
ReelGameObj* pReelGameObj;
bool startAnim;
};
#endif // __HELLOWORLD_SCENE_H__
and nothing no color in the background , why ?
im working on windows with VC 2012
I think your color is too dark. Try changing the values to (255,25,255,255) and check the result.
I created a sample project (in Beta 2), and only changed these lines:
.h:
class HelloWorld : public cocos2d::LayerColor
.cpp:
if( !LayerColor::initWithColor(Color4B(255,255,255,255)) )
The result is a white background.
I cleaned up and updated your code (I use cocos2dx 2.2.1) and I could change the layer's color to red.
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::CCLayerColor
{
public:
HelloWorld();
~HelloWorld();
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::CCScene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// a selector callback
void menuCloseCallback(CCObject* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
virtual void onEnter();
void draw();
private:
cocos2d::CCSize winSize;
cocos2d::CCSize visibleSize;
cocos2d::CCPoint origin;
bool startAnim;
};
#endif // __HELLOWORLD_SCENE_H__
The cpp file
#include "HelloWorldScene.h"
USING_NS_CC;
HelloWorld::HelloWorld()
{}
HelloWorld::~HelloWorld()
{}
CCScene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = CCScene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
ccColor4B c = ccc4(255,0,0,255);
if ( !CCLayerColor::initWithColor(c) )
{
return false;
}
return true;
}
void HelloWorld::onEnter()
{
CCLayerColor::onEnter();
}
void HelloWorld::draw()
{
CCLayerColor::draw();
}

member variables of a class has been changed, in each loop ( cocos2dx )

I 've declared some classes in my cocos2dx program, and I 've set some values for their member variables, but after each time the program loops ( the main loop of the CCDirector ), all of their values have been removed!
I want it class to save its next scene that should be replaced or pushed when the scene should be replaced (as you can see in the code, after 2 second, the introPage class calls a function "IntoPageDone" and the scene should be replaced )and I 've set a variable named nextScene for the next Scene, but after each loop, its value changes into NULL.
plz help me with this problem, and also is there some better ways to handling the scenes and changes of them??
tnx a lot!
StartingPage is a class which is inherited from CCLayerColor.
Here is CoCoGui.h file:
#ifndef _COCOGUI_H_
#define _COCOGUI_H_
#include "StartingPage.h"
#include "IntroPage.h"
using namespace cocos2d;
class CoCoGui : public CCLayerColor{
public:
CoCoGui();
virtual ~CoCoGui(void);
virtual bool init();
static CCScene* scene();
CREATE_FUNC(CoCoGui);
private:
IntroPage * introPage;
StartingPage * startingPage;
void onEnterTransitionDidFinish();
};
#endif /* COCOGUI_H */
And this is CoCoGui.cpp file:
#include "CoCoGui.h"
#include <iostream>
using namespace std;
CCScene* CoCoGui::scene(){
CCScene *scene = CCScene::create();
CoCoGui *layer = CoCoGui::create();
scene->addChild(layer);
return scene;
}
CoCoGui::CoCoGui ( )
{
this->startingPage = new StartingPage ( );
this->introPage = new IntroPage ( );
}
CoCoGui::~CoCoGui(void)
{
delete introPage;
delete startingPage;
}
bool CoCoGui::init ( ){
if ( !CCLayerColor::initWithColor ( ccc4 (100,100,100,255) ) ){
return false;
}
return true;
}
void CoCoGui::onEnterTransitionDidFinish ( ){
this->introPage->setNextScene ( StartingPage::scene( ) );
CCScene * scene = NULL;
scene = IntroPage::scene();
CCTransitionFade * trans = CCTransitionFade::create( 0.4f, scene , ccBLACK);
CCDirector::sharedDirector()->pushScene(trans);
cout << "step" << endl;
}
This is introPage.h file:
#ifndef INTROPAGE_H_
#define INTROPAGE_H_
#include "cocos2d.h"
#include "StartingPage.h"
using namespace cocos2d;
class IntroPage: public CCLayerColor {
public:
IntroPage( CCScene * nextScene );
IntroPage ( );
virtual ~IntroPage();
static CCScene* scene();
bool init();
void intoPageDone();
CREATE_FUNC(IntroPage);
CC_SYNTHESIZE_READONLY(CCLabelTTF*, _label, Label);
CCScene * getNextScene( );
void setNextScene ( CCScene * nScene );
private:
CCScene * nextScene;
};
#endif /* INTROPAGE_H_ */
And also IntroPage.cpp:
#include "IntroPage.h"
IntroPage::IntroPage( CCScene * nextScene ) {
this->introPageDone = false;
this->setNextScene ( nextScene );
}
IntroPage::IntroPage( ){
}
IntroPage::~IntroPage() {
}
void IntroPage::setNextScene ( CCScene * nScene ){
this->nextScene = nScene;
}
CCScene* IntroPage::scene(){
CCScene *scene = CCScene::create();
IntroPage *layer = IntroPage::create();
scene->addChild(layer);
return scene;
}
bool IntroPage::init() {
if (CCLayerColor::initWithColor (ccc4(0, 0, 0, 255) )) {
this->runAction(
CCSequence::actions(CCDelayTime::actionWithDuration(2),
CCCallFunc::actionWithTarget(this,
callfunc_selector(IntroPage::intoPageDone)),
NULL));
return true;
}
return false;
}
void IntroPage::intoPageDone() {
this->introPageDone = true;
CCDirector::sharedDirector( )->popScene( );
CCDirector::sharedDirector( )->pushScene( this->nextScene );
}
Finally I found the answer!
We should use static variable, so they won't destructed after main loop

After replacing Scene, the Schedule method doesn't work anymore

I'm making a game in cocos2dx , so I've made a class named CoCoGui
and I've also made an IntroPage class that inherits from CCLayerColor for the intro page of the game and a StartPage class that's been inherited from CCLayerColor, too.
I want to show the intro page for the first 2 seconds and then show the StartingPage
but in the updateGame function of CoCoGui (which is the main loop of the game), when the replaceScene method called, and the Scene become replaced, the updateGame method won't be called anymore!
Please help me with this problem
thanks!
Here's the CoCoGui.h file:
StartingPage and IntroPage are two classes that inherit from CCLayerColor
#ifndef _COCOGUI_H_
#define _COCOGUI_H_
#include "StartingPage.h"
#include "..\Classes\WorkSpace.h"
#include "..\Classes\GameBoard.h"
#include "..\Classes\IntroPage.h"
using namespace cocos2d;
class CoCoGui : public CCLayerColor{
public:
CoCoGui();
void addScene (CCScene * startPage, CCScene * work);
virtual ~CoCoGui(void);
void updateGame ( float dt );
virtual bool init();
static CCScene* scene();
CREATE_FUNC(CoCoGui);
private:
bool isInit;
CCScene * runnigScene;
IntroPage * introPage;
StartingPage * startingPage;
void onEnterTransitionDidFinish();
void menuCloseCallback(CCObject* pSender);
public:
CCScene * getRunningScene(void);
};
#endif /* COCOGUI_H */
also here is CoCoGui.cpp file
#include "CoCoGui.h"
#include <iostream>
using namespace std;
CCScene* CoCoGui::scene(){
CCScene *scene = CCScene::create();
CoCoGui *layer = CoCoGui::create();
scene->addChild(layer);
return scene;
}
CoCoGui::CoCoGui ( )
{
this->isInit = false;
this->introPage = new IntroPage ( );
this->startingPage = new StartingPage ( );
}
CoCoGui::~CoCoGui(void)
{
delete introPage;
delete startingPage;
}
void CoCoGui::menuCloseCallback(CCObject* pSender)
{
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
bool CoCoGui::init ( ){
if ( !CCLayerColor::initWithColor ( ccc4 (100,100,100,255) ) ){
return false;
}
this->schedule ( schedule_selector ( CoCoGui::updateGame ), 0.5 );
return true;
}
void CoCoGui::updateGame ( float dt ){
cout << "Update Called" << endl;
if ( !isInit )
return;
CCScene * scene = NULL;
if ( !this->introPage->isIntroPageDone ( ) ){
scene = IntroPage::scene();
}
else if ( this->introPage->isIntroPageDone ( ) ){
scene = StartingPage::scene();
}
CCDirector::sharedDirector()->replaceScene(scene);
}
void CoCoGui::onEnterTransitionDidFinish ( ){
isInit = true;
}
CCScene * CoCoGui::getRunningScene(void)
{
return this->runnigScene;
}
the ReplaceScene will trigger the this->onExit() which will trigger unschedule function.
If this is anything like cocos2d-iphone, you'll have to call the base class implementation of onEnterTransitionDidFinish and similar onEnter/onExit overrides. In cocos2d-iphone not calling super in some of these methods can cause scheduling and input to stop working.
try adding
this->resume();
after calling the schedule.
Also make sure that the scene connected to the layer is loaded. If not, it will cause a runtime error.