关于对象池问题?

官网绵羊案例中使用是cc.pool,我在学习官方文档时发现推荐cc.nodepool,于是我尝试想把绵羊案例改为cc.nodepool,后来在动画回调将节点返回对象池时出现了问题,Dust 脚本中

finish () {
        this.node.removeFromParent();
        cc.pool.putInPool(this);
        cc.log('finish')
    }

改如何修改为put,Ne.rar (184.6 KB)压缩包里为我写的DEMO,

finish () {
        //this.node.removeFromParent();
        //cc.pool.putInPool(this);
         D.dhbf.dhPool.put(this); 
        //cc.log('finish')
    }

尝试了很多,没有实现。请大神指导

问题在于你在 component 中调用 put(this),this 对象是 component,并不是 node。

你需要做的是:

D.dhbf.dhPool.put(this.node);

onEnemyKilled: function (enemy) {
// enemy 应该是一个 cc.Node
this.enemyPool.put(enemy); // 和初始化时的方法一样,将节点放进对象池,这个方法会同时调用节点的 removeFromParent
}
我试过加this.node,但是没有自动删除呀,没有同时调用节点的 removeFromParent

有的呀:

https://github.com/cocos-creator/engine/blob/master/extensions/ccpool/CCNodePool.js#L119

你的节点是还停留在界面上吗?

是的,

,一直不删除,

看了一下,实际上是会删除的,不过 animation 有点问题,在从 NodePool 中重新取出之后,无法正常播放动画,而是停留在了原来的位置上,所以动画结束事件也不会触发,导致第二次无法删除。Animation 的问题 @2youyou2 正在处理,他会给出解决方案

修改了下 dh.js, 像下面这样写就可以了
需要在 animation 真正播放完成后才能放进 pool 里,而在帧事件的回调里面 animation 还没有被 stop

cc.Class({
    extends: cc.Component,

    // use this for initialization
    onLoad: function () {
        this.node.getComponent(cc.Animation).on('stop', this.onStop, this);
    },
    finish () {
        //this.node.removeFromParent();
        //cc.pool.putInPool(this);
        // D.dhbf.dhPool.put(this.node); 
        //cc.log('finish')
    },

    onStop () {
        D.dhbf.dhPool.put(this.node);
    }
    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});

好像还是不可以呀,是不是我少了什么操作呀

在从 pool 中 get 之后,还需要手动 play(‘lift’)

我在dhpf.js 里添加了dh.getComponent(cc.Animation).play(‘lift’),dh.js里的代码和你的一样,还是不消失呀。

试下 Ne.zip (262.4 KB)

可以了,谢谢,虽然最后有一个闪烁,但可以用其他方法规避掉。