ItemGrid.ts 1.02 KB
// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class ItemGrid extends cc.Component {

    private _txtNum: cc.Label;
    private _imgIcon: cc.Sprite;

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this._imgIcon = this.node.getChildByName('icon').getComponent(cc.Sprite);
        this._txtNum = this.node.getChildByName('txtNum').getComponent(cc.Label);
    }

    start() {

    }

    // update (dt) {}

    setIcon(url: string) {
        cc.resources.load(url, cc.SpriteFrame, (error, asset) => {
            if (!error) {
                this._imgIcon.spriteFrame = asset as cc.SpriteFrame;
            }
        })
    }

    setNum(num: number) {
        this._txtNum.string = num.toString();
    }
}