cocos2d js 场景切换

发布日期:2015-11-16 10:36:14
  普通的打开一个新场景使用cc.director.runScene,在主场景打开子场景再恢复到主场景的话可以用cc.director.pushScene和cc.director.popScene



var LayerA = cc.Layer.extend({
    sprite:null,
    score : 1,
    txt : null,
    ctor:function () { this._super(); var self = this; var size = cc.winSize; var closeItem = new cc.MenuItemImage(
            res.CloseNormal_png,
            res.CloseSelected_png, function () {
                cc.director.pushScene(new SceneB());
            }, this);
        closeItem.attr({
            x: size.width / 2,
            y: 20,
            anchorX: 0.5,
            anchorY: 0.5  }); var menu = new cc.Menu(closeItem);
        menu.x = 0;
        menu.y = 0; this.addChild(menu, 1);

        cc.log("@@LayerA Init..."); //schedule  this.schedule(this.timerMove,1,cc.REPEAT_FOREVER,1); return true;
    },

    timerMove : function() { var size = cc.winSize;

        cc.log("@@timerMove..."); if(this.txt != null) this.removeChild(this.txt); this.txt = new cc.LabelTTF(this.score, "Song", 22); this.txt.attr({
            x: size.width / 2,
            y: size.height / 2  }); this.txt.setFontFillColor(cc.color(255, 255, 255)); this.addChild(this.txt); this.score++;
    }
}); var SceneA = cc.Scene.extend({
    init : false,
    onEnter:function () { this._super(); if(!this.init) { this.init = true; var layer = new LayerA(); this.addChild(layer);
        }
    }
}); var LayerB = cc.Layer.extend({
    sprite:null,
    score : 1,
    txt : null,
    ctor:function () { this._super(); var self = this; var size = cc.winSize; var closeItem = new cc.MenuItemImage(
            res.CloseNormal_png,
            res.CloseSelected_png, function () {
                cc.director.popScene();
            }, this);
        closeItem.attr({
            x: size.width / 2,
            y: 20,
            anchorX: 0.5,
            anchorY: 0.5  }); var menu = new cc.Menu(closeItem);
        menu.x = 0;
        menu.y = 0; this.addChild(menu, 1);

        cc.log("@@LayerB Init..."); return true;
    }
}); var SceneB = cc.Scene.extend({
    onEnter:function () { this._super(); var layer = new LayerB(); this.addChild(layer);
    }
});