ResWidget.ts
2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// 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
import { SaveDataManager } from "../../../component/SaveDataManager";
import { ResType, WindowName } from "../../../Global";
import { UIManager } from "../../UIManager";
const { ccclass, property } = cc._decorator;
@ccclass
export default class ResWidget extends cc.Component {
private _type: ResType = ResType.NONE;
private _txt: cc.Label;
// LIFE-CYCLE CALLBACKS:
onLoad() {
this._txt = this.node.getChildByName('txtNum').getComponent(cc.Label);
}
start() {
this.node.removeFromParent();
UIManager.ins.widgetLayer.addChild(this.node);
this.node.getComponent(cc.Widget).updateAlignment();
}
protected onEnable(): void {
this.schedule(this.onLazyUpdate, 1 / 20);
this.refresh();
}
protected onDisable(): void {
this.unschedule(this.onLazyUpdate);
}
// update (dt) {}
//#region event
onClick() {
if (CC_DEBUG && this._type == ResType.STAR) {
SaveDataManager.ins.adjustStar(5, true)
}
else {
if (this._type == ResType.DIAMOND) {
UIManager.ins.openWindow(WindowName.FREE_DIAMOND);
}
}
// CC_DEBUG && (this._type == ResType.STAR ? SaveDataManager.ins.adjustStar(5, true) : null);
}
//#endregion
//#region public method
setType(value: ResType) {
this._type = value;
this.node.name = 'widget' + this._type.toString();
this.refresh();
}
//#endregion
//#region private method
private onLazyUpdate() {
this.refresh();
}
private refresh() {
let num = (this._type == ResType.DIAMOND ? SaveDataManager.ins.runtimeData.diamond : SaveDataManager.ins.runtimeData.star);
this._txt.string = num.toString();
}
//#endregion
}