UIGameTimeCounter.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { _decorator, Component, Label, Node } from 'cc';
  2. import { GameUILayers } from '../../scripts/GameUILayers';
  3. import { ModuleDef } from '../../scripts/ModuleDef';
  4. import { GameMgr } from '../../module_basic/scripts/GameMgr';
  5. const { ccclass, property } = _decorator;
  6. /**
  7. * @en Layout component is used to mount on the root node of the UI corresponding to the Prefab, and declare a series of property references for tgxUIController to call.
  8. * @zh Layout 组件用于挂载到UI对应的Prefab根节点,并声明一系列 property 引用,供 tgxUIController 调用。
  9. */
  10. @ccclass('UIGameTimeCounter')
  11. export class Layout_UIGameTimeCounter extends Component {
  12. @property(Label) lblTips:Label;
  13. @property(Label) lblTimer: Label;
  14. }
  15. @tgx_class(ModuleDef.GAME)
  16. export class UIGameTimeCounter extends tgx.UIController {
  17. constructor(){
  18. super('ui_game_time_counter/ui_game_time_counter',GameUILayers.POPUP,Layout_UIGameTimeCounter);
  19. }
  20. public get layout():Layout_UIGameTimeCounter{
  21. return this._layout as Layout_UIGameTimeCounter;
  22. }
  23. private _endTime = 0;
  24. protected onCreated(args:{tips?:string,endTime:number}): void {
  25. if(args.tips){
  26. this.layout.lblTips.string = args.tips;
  27. }
  28. this._endTime = args.endTime || 0;
  29. }
  30. protected onUpdate(dt: number): void {
  31. let remainingTime = this._endTime - Date.now();
  32. if(remainingTime <= 0){
  33. this.close();
  34. }
  35. else{
  36. remainingTime = Math.floor(remainingTime/1000);
  37. this.layout.lblTimer.string = remainingTime.toString();
  38. }
  39. }
  40. }