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.
Related
OS : Win10
Language : c++
Cocos2d-x Ver : 3.8.1
Tool : Visual Studio 2013
//MainMenuScene.h
#ifndef ProjectV_MainMenuScene_h
#define ProjectV_MainMenuScene_h
#include "cocos2d.h"
class MainMenuScene :public cocos2d::CCLayerColor
{
public:
virtual bool init();
static cocos2d::CCScene *createScene();
CREATE_FUNC(MainMenuScene);
cocos2d::Label *PlayLabel;
void goPlayScene(cocos2d::Ref *pSender);
};
#endif
//MainMenuScene.cpp
#include "MainMenuScene.h"
#include "PlayScene.h"
USING_NS_CC;
CCScene *MainMenuScene::createScene()
{
CCScene *scene = CCScene::create();
MainMenuScene *layer = MainMenuScene::create();
scene->addChild(layer);
return scene;
}
bool MainMenuScene::init()
{
if (!CCLayerColor::initWithColor(Color4B(255, 255, 255, 255)))
{
return false;
}
PlayLabel = Label::createWithTTF("Play", "fonts/consola.ttf", 18);
PlayLabel->setColor(Color3B::BLACK);
auto PlayBtn = MenuItemLabel::create(
PlayLabel,
CC_CALLBACK_1(MainMenuScene::goPlayScene, this));
// i thought CC_CALLBACK_1(MainMenuScene::goPlayScene,this) mean call goPlayScene(Ref* pSender) when click or touch the label
PlayBtn->setPosition(Vec2(240, 100));
auto pMenu = Menu::create(PlayBtn, NULL);
pMenu->setPosition(Vec2::ZERO);
this->addChild(pMenu);
return true;
}
void MainMenuScene::goPlayScene(Ref* pSender)
{
CCScene *pScene = PlayScene::createScene();
TransitionScene *pTransScene = TransitionFade::create(1.0f, pScene, Color3B::WHITE);
Director::getInstance()->replaceScene(pTransScene);
}
i don't know why function don't call when i clicked the label
You is not passed an argument in the calling function
CC_CALLBACK_1(MainMenuScene::goPlayScene, this)
You need either remove the Ref* pSender from void MainMenuScene::goPlayScene() or add an your sender in CC_CALLBACK_1(MainMenuScene::goPlayScene, this) after this.
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();
}
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.
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
i have 2 void functions(trying to implement radio button), i want them to send value to a third function by swapping values. and that function returning value to main function?
CODE OF MY MyScene.h FILE
#ifndef __MY_SCENE_H__
#define __MY_SCENE_H__
#include "cocos2d.h"
USING_NS_CC;
class MyScene : public cocos2d::CCLayerColor
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// there's no 'id' in cpp, so we recommand to return the exactly class pointer
static cocos2d::CCScene* scene();
CCMenuItemToggle *R;
CCMenuItemToggle *L;
// a selector callback
void swapL(CCObject *sender);
void swapR(CCObject *sender);
// implement the "static node()" method manually
LAYER_NODE_FUNC(MyScene);
};
#endif // __HELLOWORLD_SCENE_H__
CODE OF MY MyScene.cpp FILE
#include "MyScene.h"
USING_NS_CC;
CCScene* MyScene::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::node();
// 'layer' is an autorelease object
MyScene *layer = MyScene::node();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool MyScene::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayerColor::initWithColor(ccc4(255,255,255,255) ))
{
return false;
}
//////////////////////////////
// 2. add your codes below...
CCSize WinSize= CCDirector::sharedDirector()->getWinSizeInPixels();
CCSprite * fish=CCSprite::spriteWithFile("fish_bg.png");
fish->setPosition(CCPointZero);
fish->setAnchorPoint(CCPointZero);
fish->setScaleX(WinSize.width/480);
fish->setScaleY(WinSize.height/395);
this->addChild(fish,0,0);
CCSprite * on1=CCSprite::spriteWithFile("on.png");
CCSprite * on2=CCSprite::spriteWithFile("on.png");
CCSprite * on3=CCSprite::spriteWithFile("on.png");
CCSprite * on4=CCSprite::spriteWithFile("on.png");
CCSprite * off1=CCSprite::spriteWithFile("off.png");
CCSprite * off2=CCSprite::spriteWithFile("off.png");
CCSprite * off3=CCSprite::spriteWithFile("off.png");
CCSprite * off4=CCSprite::spriteWithFile("off.png");
CCMenuItem *LeftOn=CCMenuItemSprite::itemFromNormalSprite(on1,on2);
CCMenuItem *RightOn=CCMenuItemSprite::itemFromNormalSprite(on3,on4);
CCMenuItem *LeftOff=CCMenuItemSprite::itemFromNormalSprite(off1,off2);
CCMenuItem *RightOff=CCMenuItemSprite::itemFromNormalSprite(off3,off4);
CCMenuItemToggle *Left = CCMenuItemToggle::itemWithTarget(this, menu_selector(MyScene::swapL),LeftOn,LeftOff,NULL);
CCMenuItemToggle *Right = CCMenuItemToggle::itemWithTarget(this, menu_selector(MyScene::swapR),RightOn,RightOff,NULL);
CCMenu *Radio= CCMenu::menuWithItems(Left,Right,NULL);
Radio->alignItemsHorizontallyWithPadding(20);
Radio->setPosition(ccp(WinSize.width/2,WinSize.height/2));
this->addChild(Radio);
//////////////////////////////
return true;
}
void MyScene::swapL(CCObject *sender)
{
L= (CCMenuItemToggle*)sender;
CCLOG("L= %d",L->getSelectedIndex());
int i=(L->getSelectedIndex());
}
void MyScene::swapR(CCObject *sender)
{
R= (CCMenuItemToggle*)sender;
CCLOG("R= %d",R->getSelectedIndex());
int j=(R->getSelectedIndex());
}
Is it possible to have 2 void functions to send arguements to a third function one each from those 2 functions ?
Yes, It's possible, Why do you think it is not possible?
Online Sample:
#include<iostream>
void doSomething(int &i)
{
i = 10;
}
void doSomethingMore(int &j)
{
j = 20;
}
void FinalDoSomething(const int i, const int j, int &k)
{
k = i + j;
}
int main()
{
int i = 0;
doSomething(i);
int j = 0;
doSomethingMore(j);
int k = 0;
FinalDoSomething(i,j,k);
std::cout<<k;
return 0;
}
You could have a method between that calls. This function stores the first call value into a member and on the second call it calls the function you want to pass the two parameters (using the previously passed one + the member