GameTips.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html
  7. import EventName from "../EventName/EventName";
  8. import MyComponent from "../Template/MyComponent";
  9. const { ccclass, property } = cc._decorator;
  10. @ccclass
  11. export default class GameTips extends MyComponent {
  12. @property(cc.Label)
  13. Hight: cc.Label = null;
  14. @property(cc.Label)
  15. Left: cc.Label = null;
  16. @property(cc.Label)
  17. Right: cc.Label = null;
  18. // LIFE-CYCLE CALLBACKS:
  19. onLoad() {
  20. super.onLoad()
  21. }
  22. start() {
  23. this.regEvent(EventName.Update_CurrentHight, this.Update_CurrentHight, this)
  24. this.regEvent(EventName.YaoGanMove, this.YaoGanMove, this)
  25. }
  26. Update_CurrentHight(hightNum: number) {
  27. this.Hight.string = `当 前 高 度 ${hightNum.toString()} M`
  28. }
  29. // YaoGanEnd(dir: cc.Vec3, power: number) {
  30. // // const result = parseFloat((power * 100).toFixed(2));
  31. // // this.History.string = `历史:${this.GetAngle(dir)}° , ${result}%`
  32. // }
  33. YaoGanMove(dir: cc.Vec3, power: number) {
  34. const result = parseFloat((power * 100).toFixed(0));
  35. this.Left.string = `角度 ${this.GetAngle(dir)}°`
  36. this.Right.string = `力量 ${this.FixLength(result)}%`
  37. }
  38. GetAngle(dir: cc.Vec3) {
  39. //根据朝向计算出夹角弧度
  40. var angle = dir.signAngle(cc.v2(0, 1));
  41. //将弧度转换为欧拉角
  42. // (angle / Math.PI * 180).toFixed(0);
  43. return this.FixLength((angle / Math.PI * 180).toFixed(0).toString())
  44. // let numStr = (angle / Math.PI * 180).toFixed(0).toString();
  45. // let spacesToAdd = 3 - numStr.length;
  46. // let paddedStr = "";
  47. // for (let i = 0; i < spacesToAdd; i++) {
  48. // paddedStr += " ";
  49. // }
  50. // return paddedStr + numStr;
  51. }
  52. FixLength(numStr) {
  53. let spacesToAdd = 4 - numStr.length;
  54. let paddedStr = "";
  55. for (let i = 0; i < spacesToAdd; i++) {
  56. paddedStr += " ";
  57. }
  58. return paddedStr + numStr;
  59. }
  60. onDestroy(): void {
  61. super.onDestroy()
  62. }
  63. }