Passing parameters to start a scene


#1

I have a game and this game has only one GameScene, when the real action happens in the game, all the others scene are menus.
But I want to implement different difficultys in my game, like ‘easy’, ‘normal’ and ‘hard’.
But I don’t want to duplicate my GameScene code to support these 3 levels type.
I want to capture a value in the init method of my GameScene and then adjust the difficulty of the game.
I’ll something like that in the init method

int level;
if(level==1)
//set difficulty to easy
if(level==2)
//set difficulty to medium
if(level==3)
//set difficulty to hard

But I want to pass this value using the replaceScene from a callback method a CCMenuItem, how I can do that?
example:

CCMenuItemFont *Button1 = CCMenuItemFont::create("Level Easy",this,menu_selector(HelloWorld::startLevelEasy));
CCMenuItemFont *Button2 = CCMenuItemFont::create("Level Medium",this,menu_selector(HelloWorld::startLevelMedium));
CCMenuItemFont *Button3 = CCMenuItemFont::create("Level Hard",this,menu_selector(HelloWorld::startLevelHard));

void HelloWorld::startLevelEasy()
{
   CCDirector::sharedDirector()->replaceScene(GameScene::scene(), 1);
}
void HelloWorld::startLevelMedium()
{
   CCDirector::sharedDirector()->replaceScene(GameScene::scene(), 2);
}
void HelloWorld::startLevelHard()
{
   CCDirector::sharedDirector()->replaceScene(GameScene::scene(), 3);
}

The numbers 1,2,3 will be value that will be checked in GameScene and based on it I’ll adjust the level.
But replaceScene don’t accept more parameters, how I can do that?


#2

Just use another static class like:
Constant.h
class Constant{ static int level = 1; }
the callback function like this:
void HelloWorld::startLevelEasy() { Constant::level = 1; CCDirector::sharedDirector()->replaceScene(GameScene::scene()); }
so when you init the scene ,you can use Constant::level to do all the thing.


#3

Your suggestion it’s very simple and efficient
Thanks for your help


#4

I have a little problem with the code that you write
this is my file
Constant.h

class Constant {
public:
    static int level;
};

and this is my method to call the replaceScene

void HelloWorld::startLevelEasy()
{
      Constant::level=1;
      CCDirector::sharedDirector()->replaceScene(GameScene::scene());
}

But I receive the following error inside method startLevelEasy()

Undefined reference to 'Constant::level'

Why this is happening?
It’s necessary to create a Constant.cpp file?


#5

I think you should create a Constant.cpp…
Or may be like this:
Constant.h

class Constant {
public:
    static int level;
};

HelloWorld.cpp

#include"Constant.h"
void HelloWorld::startLevelEasy()
{
      Constant::level=1;
      CCDirector::sharedDirector()->replaceScene(GameScene::scene());
}
int Constant::level = 1;

But I think you had better to create a Constant.cpp file.


#6

HI, I used a mix of what you say.

Constant.h

class Constant {
public:
    static int level;
};
int Constant::level = 1;

It worked for me, thanks for help


#8

There is another way to accomplish that
just save the Level info with following code

UserDefault::getInstance()->setIntegerForKey(“Level”,2);

and get it everywhere with following code
int level =UserDefault::getInstance()->getIntegerForKey(“Level”);


#9

@farid is right, we have a tutorial on this topic https://www.youtube.com/watch?v=_FYsuSFKCZU&list=PLRtjMdoYXLf4vzkg0wdBrp7YoOThOLf4H&index=15


#10

Hey, what do you guys think about this approach of passing data to a Scene? I personally have a problem with static fields/Singleton classes so I’d much rather construct the scene with parameters.

This example passes the “level” parameter to the GameScene:

Header

class GameScene : public cocos2d::Scene
{
	int level;
public:
	GameScene(int level);

	virtual bool init();
};

Source

GameScene::GameScene(int level)
{
	//initialize the scene
	this->level = level;

        //perform cocos2d-x specific init stuff
	if (init())
	{
		autorelease();
	}
}

bool GameScene::init()
{
	bool success = false;
	if (Scene::init())
	{
		success = true;
	}

	return success;
}

How to Pass DATA/values from oneClass.cpp to AnotherClass.cpp
#11

But I have a different issue i want to select an image from one scene and want retrieve that in other
scene can anybody help me ?


#12

Yes, it’s possible to pass parameter by following step:
Step 1. Comment the create macro at header file:
//CREATE_FUNC(MySceneClass);
Step 2. Implement create function our self:
.h
static MySceneClass* create(int iParam);
.cpp
MySceneClass* MySceneClass::create(int iParam) {
MySceneClass*pRet = new(std::nothrow) MySceneClass(iParam);
if (pRet && pRet->init()) {
pRet->autorelease();
return pRet;
} else {
delete pRet;
pRet = nullptr;
return nullptr;
}
}
Step 3. Implement our self constructor to take parameter passing
.h
MySceneClass(int iParam);
.cpp
MySceneClass::MySceneClass(int iParam) {
m_iParam = iParam;//in here we got parameter passing
}
Step 4: Using
auto pNewScene = MySceneClass::create(123);