Cocos2d-JS切换场景与切换特效

var HelloWorldLayer = cc.Layer.extend({
    sprite:null,
    ctor:function () {
        //////////////////////////////
        // 1. super init first
        this._super();
        
       var size=cc.winSize;
       //旧场景
       var background=new cc.Sprite(res.CloseNormal_png);
       background.setPosition(size.width/2,size.height/2);
       this.addChild(background);
       
       this.scheduleOnce(function(f){
          //创建新场景
           var newScene=new cc.Scene();
           var newBackground=new cc.Sprite(res.HelloWorld_png);
           newBackground.setPosition(size.width/2,size.height/2);
           newScene.addChild(newBackground);
           //替换场景
           cc.director.runScene(newScene);
       },3);
       
        return true;
    }
});

var HelloWorldScene = cc.Scene.extend({
    onEnter:function () {
        this._super();
        var layer = new HelloWorldLayer();
        this.addChild(layer);
    }
});

 

场景切换特效:

//渐变替换
var transition=new cc.TransitionCrossFade(1,newScene);
cc.director.runScene(transition);

 

  //翻页替换
  var transition=new cc.TransitionPageTurn(1,newScene,false);
  cc.director.runScene(transition);

....

 

 

注意:

coocs2d-js 3.x 之后场景切换用的是 cc.director.runScene  , cc.director.replaceScene在浏览器测试会失效

posted @ 2016-01-20 22:54  tinyphp  Views(5721)  Comments(0Edit  收藏  举报