Functions for replace scene

Hello.
I faced strange issue in cocos2d js.
In the first scene,the game change running scene to the second scene using pushScene function.
But when the game call popScene function in the second scene,the running scene didn’t changed into first scene what called pushScene function.
If any developer knows about it,then please help me.
Thanks.

First Scene

var IntroLayer = cc.Layer.extend({
sprite:null,
ctor:function(){
this._super();
var size = cc.winSize;
var winSize = cc.winSize;
var background = new cc.Sprite(res.temno_png, new cc.Rect(0, 0, winSize.width, winSize.height));
background.setPosition(new cc.p(winSize.width/2,winSize.height/2));
this.addChild(background);
background.setScale(0.5);
return true;
},
onEnter:function(){
this._super();
this.getDailyBonus();
},

getDailyBonus:function () {
    var hasdailyBonus = false;
    var buf;
    for (var i = 0; i < 30; i++)
    {
        buf = "day_bonus" + (i + 1);
        if (cc.sys.localStorage.getItem(buf) == 1)
        {
            hasdailyBonus = true;
            break;
        }
    }
    var scene = new DailyBonusScene();
  //var transition = new cc.TransitionCrossFade()
    cc.director.pushScene(scene);
}

});

var IntroScene = cc.Scene.extend({
ctor:function () {
this._super();
var layer = new IntroLayer();
this.addChild(layer);
}
});

##Second Scene##

var DailyBonusLayer = cc.Layer.extend({
selectedItem : null,
tutorTime : 0.0,
tutorTTF : null,
temnoBg :null,
characterArray : null,
sPoint:null,
ePoint:null,
cPoint:null,
touchStatus : false,
bonusPad : null,
curOrder : 0,
start_pos_x : 0.0,
tutor_temno : null,
tutorSp : null,
ballonSp:null,
tutorShineSp:null,
particle:null,
ctor:function () {
//////////////////////////////
// 1. super init first
this._super();
// setTouchEnabled(true);
var winSize = cc.winSize;
var backSp = new cc.Sprite(res.background_jpg);
var fX = winSize.width / backSp.getContentSize().width;
var fY = winSize.height / backSp.getContentSize().height;
backSp.setScaleX(fX);
backSp.setScaleY(fY);
this.addChild(backSp , 0 , 10);
backSp.setPosition(new cc.p(winSize.width / 2, winSize.height - backSp.getBoundingBox().height / 2));

    bonusPad = new cc.Sprite(res.bonus_pad_png);
    this.addChild(bonusPad);
    bonusPad.setScale(0.7);
    bonusPad.setPosition(new cc.p(winSize.width / 2, winSize.height / 2));
    {
        var topSp = new cc.Sprite("#return_bonus_top_1.png");
        var animation_top = cc.animationCache.getAnimation("ReturnBonusTop");
        topSp.runAction(new cc.repeatForever(new cc.animate(animation_top)));
        bonusPad.addChild(topSp);
        topSp.setPosition(new cc.p(459, 515));

        var topTitle = new cc.LabelBMFont("RETURN BONUS","fonts/Gold_1.fnt");
        bonusPad.addChild(topTitle);
        topTitle.setPosition(new cc.p(484, 566));
        topTitle.setScale(0.5);
    }

    var leftSp = new cc.Sprite(res.background_jpg, new cc.Rect(0,0,(winSize.width/2 - (bonusPad.getContentSize().width/2 - 26) * /*fScaleN*/1 ) / fX , 721));
    leftSp.setScaleX(fX);
    leftSp.setScaleY(fY);
    this.addChild(leftSp, 10);
    leftSp.setPosition(new cc.p(leftSp.getBoundingBox().width/2,winSize.height/2));

    leftSp = new cc.Sprite(res.bonus_pad_png, new cc.Rect(0, 0, 26, 627));
    leftSp.setScale(fScaleN);
    this.addChild(leftSp, 10);
    leftSp.setPosition(new cc.p(bonusPad.getPositionX() - bonusPad.getBoundingBox().width/2 + leftSp.getBoundingBox().width/2, winSize.height / 2));

    var rightPos = (winSize.width / 2 + (bonusPad.getContentSize().width / 2 - 28) * /*fScaleN*/1) / fX;
    var rightSp = new cc.Sprite(res.background_jpg,new cc.Rect(rightPos, 0, 1280-rightPos, 721));
    rightSp.setScaleX(fX);
    rightSp.setScaleY(fY);
    this.addChild(rightSp, 10);
    rightSp.setPosition(new cc.p(winSize.width - rightSp.getBoundingBox().width / 2, winSize.height / 2));

    rightSp = new cc.Sprite(res.bonus_pad_png, new cc.Rect(936, 0, 36, 627));
    //rightSp.setScale(fScaleN);
    this.addChild(rightSp, 10);
    rightSp.setPosition(new cc.p(bonusPad.getPositionX() + bonusPad.getBoundingBox().width / 2 - rightSp.getBoundingBox().width / 2, winSize.height / 2));

    var closeSp =new cc.Sprite("#menu_close.png");
    //closeSp.setOpacity(0);
    var closeBtn = new cc.MenuItemSprite(closeSp, null, this.backAction,this);
    closeBtn.setPosition(new cc.p(938, 594));
    var closeMnu = new cc.Menu(closeBtn);
    closeMnu.setPosition(new cc.p(0,0));
    bonusPad.addChild(closeMnu, 1);
    return true;
},
backAction:function(pSender){
   // if (!sfxOff)
     //   cc.audioEngine.playEffect(sName[kSFXMenuButtonClick]);
    if (cc.sys.localStorage.getItem("Tutor") == 1)
        return;
    var buf;
    for (var i = 0; i < 30; i++)
    {
        buf = "day_bonus" + (i + 1);
        if (cc.sys.localStorage.getItem(buf) == 1)
        {
            var bonus = bonus_base[i] * (1.1 + _cData.stageCount * 0.04 )*( 1.1 );
            _cData.memload += bonus;
            cc.sys.localStorage.setItem(buf, 2);
        }
    }
   cc.director.popScene();
}

});

var DailyBonusScene = cc.Scene.extend({
ctor:function () {
this._super();
var layer = new DailyBonusLayer();
this.addChild(layer);
}
});

@jinjin

If you want to replace current scene, use this

cc.director.runScene(new SceneClassName()); 

In your case

cc.director.runScene(new IntroScene()); 
cc.director.runScene(new DailyBonusScene()); 

Don’t use push scene if not needed as it may caused memory issue.

thanks for your response.
But when second scene changed into first scene,the first scene should keep its state before changed into second scene.
In this case,how to use runscene function?

@jinjin

Have you try to use runScene with your current code?Does it work?

If you want to remember the scene state, you may need to use

  1. pushScene
  2. use global variables to remember the scene state and pass in when creating the scene

I have tested the pushScene and popScene functions, working fine in v3.6.1.

Which version are you using?

i’m using 3.6.1 as well.
please check my code again and inform me what is wrong.
thanks

@jinjin you can surround your code with “code” inside two brackets to make your code more readable for the readers
Like this:
Use [] instead of ()

(code)

then

(/code)

can’t u read my code?

Yes of course, no problem there. I’m just letting you know that it could be a good thing to do, take a look:

Without any surrounding:

var IntroLayer = cc.Layer.extend({
sprite:null,
ctor:function(){fd
this._super();
var size = cc.winSize;
var winSize = cc.winSize;
var background = new cc.Sprite(res.temno_png, new cc.Rect(0, 0, winSize.width, winSize.height));
background.setPosition(new cc.p(winSize.width/2,winSize.height/2));
this.addChild(background);
background.setScale(0.5);
return true;
},
onEnter:function(){
this._super();
this.getDailyBonus();
},
getDailyBonus:function () {
var hasdailyBonus = false;
var buf;
for (var i = 0; i < 30; i++)
{
buf = “day_bonus” + (i + 1);
if (cc.sys.localStorage.getItem(buf) == 1)
{
hasdailyBonus = true;
break;
}
}
var scene = new DailyBonusScene();
//var transition = new cc.TransitionCrossFade()
cc.director.pushScene(scene);
}

});

var IntroScene = cc.Scene.extend({
ctor:function () {
this._super();
var layer = new IntroLayer();
this.addChild(layer);
}
});

With the brackets:

var IntroLayer = cc.Layer.extend({
 sprite:null,
 ctor:function(){
 this._super();
 var size = cc.winSize;
 var winSize = cc.winSize;
 var background = new cc.Sprite(res.temno_png, new cc.Rect(0, 0, winSize.width, winSize.height));
 background.setPosition(new cc.p(winSize.width/2,winSize.height/2));
 this.addChild(background);
 background.setScale(0.5);
 return true;
 },
 onEnter:function(){
 this._super();
 this.getDailyBonus();
 },
getDailyBonus:function () {
    var hasdailyBonus = false;
    var buf;
    for (var i = 0; i < 30; i++)
    {
        buf = "day_bonus" + (i + 1);
        if (cc.sys.localStorage.getItem(buf) == 1)
        {
            hasdailyBonus = true;
            break;
        }
    }
    var scene = new DailyBonusScene();
  //var transition = new cc.TransitionCrossFade()
    cc.director.pushScene(scene);
}

});

var IntroScene = cc.Scene.extend({
 ctor:function () {
 this._super();
 var layer = new IntroLayer();
 this.addChild(layer);
 }
});

It just makes it more simple and readable.