// run the scene cc.director.runScene(scene); // get the running scene var scene = cc.director.getRunningScene(); // push in a new scene and cover the running scene var scene = cc.director.pushScene(scene); // pop the top scene and run the second scene cc.director.popScene(); // pop all the scene except the scene in the bottom cc.director.popToRootScene()
var menuLayer=cc.Layer.extend({ ctor:function(){ this._super(); // add text var tit=new cc.LabelTTF("CVP认证","",60); tit.setPosition(240,300); this.addChild(tit); tit.setTag(1); // add menu // click to add a text,to test if the scene destroyed when director invoke runScene var clickItem = new cc.MenuItemFont("添加文字",this.clickCallback,this); var itemstart=new cc.MenuItemFont("开始",this.startScene,this); itemstart.setPosition(clickItem.getPositionX(),clickItem.getPositionY()-clickItem.height/2-50); var itemhelp=new cc.MenuItemFont("帮助",this.helpScene,this); itemhelp.setPosition(itemstart.getPositionX(),itemstart.getPositionY()-itemstart.height/2-50); var menu=new cc.Menu(itemstart,itemhelp,clickItem); this.addChild(menu); }, startScene:function(){ cc.director.runScene(new HelloWorldScene()); }, helpScene:function(){ cc.director.runScene(new HelpScene()); }, clickCallback:function(){ // add text var test = new cc.LabelTTF("添加的文字","",50); var tit = this.getChildByTag(1); test.setPosition(tit.getPositionX(),tit.getPositionY()+100); this.addChild(test); } }); // define the menu scene var MenuScene=cc.Scene.extend({ ctor:function(){ this._super(); // define the layer var m1=new menuLayer(); this.addChild(m1); } });
var HelloWorldLayer = cc.Layer.extend({ sprite:null, ctor:function () { this._super(); var helloLabel = new cc.LabelTTF("Hello World", "Arial", 38); helloLabel.x = size.width / 2; helloLabel.y = size.height / 2 + 200; this.addChild(helloLabel, 5); this.sprite = new cc.Sprite(res.HelloWorld_png); this.sprite.attr({ x: size.width / 2, y: size.height / 2 }); this.addChild(this.sprite, 0); var backItem = new cc.MenuItemFont("返回主界面",this.backCallback,this); var menu = new cc.Menu(backItem); this.addChild(menu); returntrue; }, backCallback:function(){ cc.director.runScene(new MenuScene()); } }); var HelloWorldScene = cc.Scene.extend({ onEnter:function () { this._super(); var layer = new HelloWorldLayer(); this.addChild(layer); } });
这是一个帮助场景,场景中只有一行文本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
var HelpLayer = cc.Layer.extend({ ctor:function(){ this._super(); var text = new cc.LabelTTF("游戏帮助场景","",100); text.setPosition(size.width/2,size.height/2); this.addChild(text); returntrue; } });
var HelpScene = cc.Scene.extend({ ctor:function(){ this._super(); var helpLayer = new HelpLayer(); this.addChild(helpLayer); returntrue; } });