Cocos Cretator 教程:自定義cc.Class

有時候想在繼承cc.Component中的properties中使用自定義的類對象。這個時候就需要使用 cc.Class 聲明創建出所需要的類。

代碼

//自定義類
let TestItem = cc.Class({
    name: 'TestItem',
    properties: {
        id: 0,
        name: ''
    }
});


cc.Class({
    extends: cc.Component,

    properties: {
        user: [TestItem],
        label: {
            default: null,
            type: cc.Label
        },
        // defaults, set visually when attaching this script to the Canvas
        text: 'Hello, World!'
    },

    // use this for initialization
    onLoad: function () {
        this.label.string = this.text;
        //查看類
        console.log('user=', this.user);
    },

    // called every frame
    update: function (dt) {

    },
});

效果

效果

最後

最後,喜歡就動動手指點喜歡,關注我吧。我會不定時更新Cocos Creator教程哦~666 →_→

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章