官方案例Duang Sheep遇到的问题总结。

新学习Cocos Creator。下载了官方DEMO。
用V2.0.1打开后,发现,这个DEMO是老版本写出来的(不知道现在更新没有)。
遇到的问题在这里说一下,新老版本对比一下。加深一下学习印象。
主要有以下问题。
1,就是属性 老版本用的url ,2.0.1后得改成 type。主要就是Sheep.js文件里面的几个音频属性。
2,就是事件 cc.eventManager不推荐使用了。改为了使用cc.systemEvent.on(键盘消息)和this.node.parent.on(触摸消息)。
老代码
cc.eventManager.addListener({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
onTouchBegan: function(touch, event) {
this.jump();
return true;
}.bind(this)
}, this.node);
我改的代码,我registerInput () 函数弃用了。
enableInput: function (enable) {
if (enable) {
cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN,
this.onKeyDowns,this);

       this.node.parent.on(cc.Node.EventType.TOUCH_START,this.onTouch,this
    } else {
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN,this.onKeyDowns,this);
        this.node.parent.off(cc.Node.EventType.TOUCH_START,this.onTouch
        ,this)
    }
},

3,然后就是cc.pool得改成 cc.NodePool了。有两处要改,一处是PipeGroupManage.js里,管道生成,一处是Sheep里跳起时烟尘动画的生成。
cc.pool看了代码和文档。感觉挺好的。就像一个性奴,随性随地都可以拿来嗨皮一下。
cc.NodePool好像是花样多。但是用法就不太一样了。
我是这样搞的。先在属性里声明一个 pipePool:cc.NodePool;
然后把那个 spwanPipe函数改成了这样
spawnPipe () {
if (this.pipePool == null ){
this.pipePool = new cc.NodePool();
}
let inicount = 1;
if (this.pipePool.size() < 1){
for (let i = 0; i < inicount; i++){
let eumpipe = cc.instantiate(this.pipePrefab);
this.pipePool.put(eumpipe);
}
}
let pipetem4 = this.pipePool.get();
if (pipetem4 != null){
this.pipeLayer.addChild(pipetem4);
this.pipeLayer.x = this.initPipeX;
}
},
原先的代码是这样的。
spawnPipe () {
if (cc.Pool.hasObject(PipeGroup)) {

        pipeGroup = cc.Pool.getFromPool(PipeGroup);
    } else {
        pipeGroup = cc.instantiate(this.pipePrefab).getComponent(PipeGroup);
    }
    this.pipeLayer.addChild(pipeGroup.node);
    pipeGroup.node.active = true;
    pipeGroup.node.x = this.initPipeX;
    
},

还有就是Sheep.js里的spawnDust函数。也是在属性里声明了NodePool
我给改成样样了。
spawnDust (animName) {
if (this.dust == null) {
this.dust = new cc.NodePool();
}

    let countdust = 2;
    if (this.dust.size() < 1) {
    for (let i = 0 ;i < countdust;i++){
       let temdust = cc.instantiate(this.dustPrefab);
        this.dust.put(temdust);
    }
}

    let a = this.dust.get();
    this.node.parent.addChild(a);
    let b = a.getComponent(Dust);
    b.node.position = this.node.position;
    b.playAnim(animName,this.dust);
}

原代码是这样的。
spawnDust (animName) {
if (cc.pool.hasObject(Dust)) {
dust = cc.pool.getFromPool(Dust);
} else {
dust = cc.instantiate(this.dustPrefab).getComponent(Dust);
}
this.node.parent.addChild(dust.node);
dust.node.position = this.node.position;
dust.playAnim(animName);
}
}
这个涉及到一个Dust.js文件里 playAnim(animName).我把他改成playAnim(animName, ccNodePool)了。
还有就是PipeGrop.js里面 onEnable里面加了一句代码。this.node.x= 0;
因为如果不这样,放回到对像池里的对像的x 就会是消失时的X。就不会再显示出来了。