Cocos Creator 构造函数传参警告 Can not instantiate CCClass 'Test' with arguments.

Posted gamedaybyday

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Cocos Creator 构造函数传参警告 Can not instantiate CCClass 'Test' with arguments.相关的知识,希望对你有一定的参考价值。

版本2.3.4:

 

在cocos中,自定义的类如果在构造函数里传参数,会有警告提示。

 

例如下面的类,在构造函数传入a,b参数

Test.ts

const {ccclass, property} = cc._decorator;

@ccclass
export default class Test extends cc.Component {

    private a:number;
    private b:number;
    constructor(a:number, b:number) {
        super();
        this.a = a;
        this.b = b;
    }
}

 

会有警告提示

技术图片

 

 

 

查阅资料,在论坛里找到这样的描述。只能用arguments来获取参数。

技术图片

 

 

 

 

更改如下,警告信息消失,并且参数可以正常传递

const {ccclass, property} = cc._decorator;

@ccclass
export default class Test extends cc.Component {

    private a:number;
    private b:number;
    constructor(...params:any) {
        super();
        this.a = params[0];
        this.b = params[1];
        console.log(this.a, this.b);//输出1,2
    }
}

  

let test = new Test(1,2);

  

以上是关于Cocos Creator 构造函数传参警告 Can not instantiate CCClass 'Test' with arguments.的主要内容,如果未能解决你的问题,请参考以下文章

cocos creator回调函数怎么用

cocos creator自学心得------脚本学习第一天

cocos creator学习01 关于cocos creator 通过get 和post连接node.js服务器的初步探索

Cocos Creator3【Cocos引擎常用的自执行函数】

Cocos creator之javascript闭包

五子棋人机博弈游戏(cocos creator)