The thing you want to instantiate is nil addChild: The child to add must be instance of cc.Node, not

Cocos开发采坑:

错误还原:

添加预制体,在onLoad调用生成预制体;运行报错,如下

代码如下:

// 排行榜预制体
    @property(cc.Prefab)
    RankingPrefab: cc.Prefab = null;

    nodePHB: cc.Node = null;
    leaderBoard: cc.Node = null;



    onLoad() {
        cc.log("onload")
        this.nodePHB = cc.find("Canvas/NodePHB")
        this.nodePHB.active = false;
        this.leaderBoard = cc.find("ScrollView/view/content", this.nodePHB);
        this.addRankingItem()

    }
    clickBtn(sender: cc.EventTarget, data: String) {
        if (data === 'play') {
            cc.log("点击了开始")
        } else if (data === 'phb') {
            cc.log("点击了排行榜")
            this.nodePHB.active = true;
        } else if (data = "closePHB") {
            cc.log("点击了排行榜关闭按钮")
            this.nodePHB.active = false;
        }
    }
    // 创建 排行榜单个
    addRankingItem() {
        
        let node = cc.instantiate(this.RankingPrefab);
        this.leaderBoard.addChild(node)
    }

阿西吧,之前开发都没有这种情况,后面排查了发现,在游戏加载的时候,onload会被执行两次。

执行第一次的时候,资源没有加载完成,可能哈!!!,执行的第二次的时候,资源才加载完成,因此,预制体自然没有加载成功!父节点页面有加载成功,导致出现上面的错误!,解决方案,只需要加入一行代码

  // 创建 排行榜单个
    addRankingItem() {
        if (this.RankingPrefab == null) return;//解决方案
        let node = cc.instantiate(this.RankingPrefab);
        this.leaderBoard.addChild(node)
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值