Pass parameters to new scene

  • barabasandras1
    Likes 0

    Problem Description

    What is the best/fastest/easiest way to pass parameters to another scene after we push it? Here is my example code:

    ...
    this.scheduleOnce(this.goToScene2, 3.0);
    …,
    goToScene2: function()
    {
        var scene = new Scene2Scene();
            
        cc.director.runScene(scene);
    }

    If I created an object in Scene1Scene, how can I still see/use it in Scene2 (which goToScene2 refers to).

    Edit: I know if I declare the object as global object it will work and I will be able to use it in other scenes, but is there any other solution than? (var vs. without var)


  • Sonar Systems admin
    Likes 0

    check this out http://discuss.cocos2d-x.org/t/audio-toggle-pass-variable-between-scenes-in-cocos2d-html5-javascript/8998/5

  • barabasandras1
    Likes 0

    MenuItemToggle, I don’t even know what that is. What if I have to pass a custom class instance? Not some menuitem thing.

  • Sonar Systems admin
    Likes 0

    what are you trying to share

  • barabasandras1
    Likes 0

    I’m creating a player(name, score) instance in scene 1, which must be available in scene 2 also.

  • Sonar Systems admin
    Likes 0

    try using userdefault to store data

  • barabasandras1
    Likes 0

    Well that’s an option too though. But can you store objects there like key-value pairs? Or just simple string, int etc.

  • Sonar Systems admin
    Likes 0

    simple data like string and integers

  • barabasandras1
    Likes 0

    Well that’s not good enough.

    But is it possible to pass parameters in the scene constructor? 

    auto scene = GameScene::createScene(“something goes here”);
    

           


  • Sonar Systems admin
    Likes 0

    I thought you was using cocos2d-JS not cocos2d-x C++

  • barabasandras1
    Likes 0

    Sorry about that, I’m using JS I just wanted to write an example. 

  • Sonar Systems admin
    Likes 0

    i know how to do it in cocos2d-x C++

  • barabasandras1
    Likes 0

    Would you mind sharing it? :D

  • Sonar Systems admin
    Likes 0

    Scene to receive variable header

    static cocos2d::Scene *createScene( int phaseNumberTemp, int levelNumberTemp );
    

    Scene to receive variable implementation

    // the world and level number
    int phaseNumberGS;
    int levelNumberGS;
    
    Scene *GameScene::createScene( int phaseNumberTemp, int levelNumberTemp )
    {
        // store world and level number
        phaseNumberGS = phaseNumberTemp;
        levelNumberGS = levelNumberTemp;
        
        // 'scene' is an autorelease object
        auto scene = Scene::createWithPhysics();
        
        scene->getPhysicsWorld( )->setDebugDrawMask( PhysicsWorld::DEBUGDRAW_ALL );
        
        // 'layer' is an autorelease object
        auto layer = GameScene::create();
        layer->setPhysicsWorld(scene->getPhysicsWorld());
    
        // add layer as a child to scene
        scene->addChild(layer);
    
        // return the scene
        return scene;
    }

    Calling the scene and passing parameters

    // go to settings scene
    Scene *scene = GameScene::createScene( 1, 2  );
        
    Director::getInstance( )->replaceScene( TransitionFade::create( 1.0, scene ) );

     

  • barabasandras1
    Likes 0

    I managed to do it like this:

    var Scene1Layer = cc.LayerColor.extend({
        ctor:function()
        {
            this._super();
            
            var player = new Player("Player1", 100);
            
            this.goToScene2(player);
            
            return true;
        },
        goToScene2: function(player)
        {
            var scene = new Scene2Scene(player);
            
            cc.director.runScene(scene);
        }
    });

     

    var Scene2Scene = cc.Scene.extend({
        onEnter:function()
        {
            this._super();

            if (SCENE2_INIT == false)
            {
                SCENE2_INIT = true;

                var layer = new Scene2Layer();

                this.addChild(layer);
            }
        },
        ctor:function(player)
        {
            this._super();
            
            cc.log(player.age);
        
            return true;
        }
    });


    This reply has been verified.
  • Sonar Systems admin
    Likes 1

    Fantastic, hope others can benefit from your knowledge

  • Sonar Systems admin
    Likes 0

    Also got this solution which is very similar to yours laugh

    Scene1

    var Scene1 = cc.Scene.extend(
    {
        ctor:function ()
    	{
    		this._super();
    		this.init();
    	},
    
    	init:function ()
    	{
            var size = cc.winSize;
            // add a "close" icon to exit the progress. it's an autorelease object
            var closeItem = new cc.MenuItemImage(
                res.CloseNormal_png,
                res.CloseSelected_png,
                function () {
                    var scene = new Scene2(5, 78.6);
                    cc.director.replaceScene(scene);
                }, this);
            closeItem.attr({
                x: size.width - 20,
                y: 20,
                anchorX: 0.5,
                anchorY: 0.5
            });
            var menu = new cc.Menu(closeItem);
            menu.x = 0;
            menu.y = 0;
            this.addChild(menu, 1);
    	}
    });
    

    Scene2

    var Scene2 = cc.Scene.extend(
    {
        ctor:function (var1, var2)
    	{
    		this._super();
    		this.init(var1, var2);
    	},
    
    	init:function (var1, var2)
    	{
            var size = cc.winSize;
    
            var helloLabel = new cc.LabelTTF("Hello World", "Arial", 38);
            helloLabel.x = size.width / 2;
            helloLabel.y = size.height / 2;
            this.addChild(helloLabel, 5);
    
            cc.log("Var 1: " + var1);
            cc.log("Var 2: " + var2);
    	}
    });

     

     

  • barabasandras1
    Likes 0

    This is how I do actually :D I just don’t use the init though. Isn’t that something that just remained in cocos2d-x from cocos2d? With ctor added I don’t really find it useful anymore.

  • Sonar Systems admin
    Likes 1

    True not needed :D

Login to reply