为什么NodePool.put()会出现问题?

尝试修改代码运用新的NodePool但是遇到了这个问题。

报错
Uncaught TypeError: obj.removeFromParent is not a function

代码
const PipeGroup = require(‘PipeGroup’);

    cc.Class({
        extends: cc.Component,

        properties: {
            pipePrefab: cc.Prefab,
            pipeLayer: cc.Node,
            initPipeX: 0,
            spawnInterval: 0
        },


        onLoad () {
            Global.pipeManager = this;
            this.pipeGroupPool = new cc.NodePool();
        },

        startSpawn () {
            this.spawnPipe();
            this.schedule(this.spawnPipe, this.spawnInterval); // spawnInterval in seconds
        },

        spawnPipe() {
            let pipeGroup = null;

            if (this.pipeGroupPool.size() > 0) {
                pipeGroup = this.pipeGroupPool.get();
            } else {
                pipeGroup = cc.instantiate(this.pipePrefab).getComponent(PipeGroup);
            }

            this.pipeLayer.addChild(pipeGroup.node);
            pipeGroup.node.active = true;
            pipeGroup.node.x = this.initPipeX;
        },

        destroyPipe(pipe) {
            // pipe.node.removeFromParent();
            console.log(typeof(pipe), pipe);
            pipe.node.active = false;
            this.pipeGroupPool.put(pipe);
        },

        stop() {
            this.unschedule(this.spawnPipe);
        }

    });

首先,我怀疑你put(pipe)这里的pipe不是node

1赞

你上一句
pipe.node.active
下一句put(pipe)
两句有一句是错的

我之前也有怀疑这个,但是不确定是不是。所以之前我试了console.log查看,只知道是Object。然后里面有个node
我尝试了这行代码,也报错。

代码
this.pipeGroupPool.put(pipe.node);

报错
addChild: The child to add must be instance of cc.Node, not .

已经尝试移除pipe.node.active,还是报错。

你这新发出来的报错不是addchild那里吗

开头为啥要引用PipeGroup,这应该是你预制体的脚本组件,删掉这个引用
还有实例化prefab那里,后面getComponent不要了
这样子后面pipeGroup就是一个node,
不用写成pipeGroup.node

已经修改但是还是得到一样的报错。 obj.removeFromParent is not a function

cc.Class({
    extends: cc.Component,

    properties: {
        pipePrefab: cc.Prefab,
        pipeLayer: cc.Node,
        initPipeX: 0,
        spawnInterval: 0
    },


    onLoad () {
        Global.pipeManager = this;
        this.pipeGroupPool = new cc.NodePool();
    },

    startSpawn () {
        this.spawnPipe();
        this.schedule(this.spawnPipe, this.spawnInterval); // spawnInterval in seconds
    },

    spawnPipe() {
        let pipeGroup = null;

        if (this.pipeGroupPool.size() > 0) {
            pipeGroup = this.pipeGroupPool.get();
        } else {
            pipeGroup = cc.instantiate(this.pipePrefab);
        }

        this.pipeLayer.addChild(pipeGroup);
        pipeGroup.active = true;
        pipeGroup.x = this.initPipeX;
    },

    destroyPipe(pipe) {
        pipe.active = false;
        this.pipeGroupPool.put(pipe);
    },

    stop() {
        this.unschedule(this.spawnPipe);
    }

});

报错后直接停止

再把active=false去掉呢?

一样的报错

我也不知道了。。。
看代码没问题啊,只有你自己打印出来查了,,,是不是其他脚本里对这个node干了什么事情

我也不知道,再查查看吧。对了请问哪里可以找到比较新的cocos creator实例。我找到的都是旧的,好像都没新的。

没多少吧,还是根据需求找功能性的例子教程吧,好找

好的,谢谢。:grin:

已经解决该问题,问题在PipeGroup.js 文件里。感谢 @99512257 的帮助 :grinning:

cc.Class({
    extends: cc.Component,

    properties: {
        speed: 0,
        botYRange: cc.p(0, 0),
        spacingRange: cc.p(0, 0),
        topPipe: cc.Node,
        botPipe: cc.Node
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    // start () {

    // },

    onEnable () {
        let botYPos = this.botYRange.x + Math.random() * (this.botYRange.y - this.botYRange.x);
        let space = this.spacingRange.x + Math.random() * (this.spacingRange.y - this.spacingRange.x);
        let topYPos = botYPos + space;
        this.topPipe.y = topYPos;
        this.botPipe.y = botYPos;
    },

    update (dt) {
        this.node.x += this.speed * dt;

        var disappear = this.node.getBoundingBoxToWorld().xMax < 0;
        if (disappear) {
            Global.pipeManager.destroyPipe(this.node); // 之前是用this,修改成this.node就没问题了
        }
    },
});