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:
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?
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.
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;
}