Cube_Infor.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { _decorator, Component, Label, Node, RigidBody } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. export enum Cube_State {
  4. live,
  5. wait,
  6. dead
  7. }
  8. @ccclass('Cube_Infor')
  9. export class Cube_Infor extends Component {
  10. @property(Label)
  11. txt: Label;
  12. @property(Label)
  13. txt2: Label;
  14. @property(RigidBody)
  15. rigidbody: RigidBody;
  16. text: string;
  17. state: Cube_State = Cube_State.live;
  18. // 添加缓存状态来避免重复切换
  19. isUp:boolean=false;
  20. start() { }
  21. update(deltaTime: number) {
  22. // 获取方块当前的旋转角度(以欧拉角形式)
  23. let rotationX = this.node.eulerAngles.x;
  24. if (rotationX >= -160 && rotationX < -30) {
  25. this.ShowFront(); // 显示正面字
  26. } else if(rotationX > 30 && rotationX <= 160) {
  27. this.ShowBack(); // 显示反面字
  28. }else {
  29. this.ShowBothSides();
  30. }
  31. }
  32. ShowFront() {
  33. this.txt.node.active = true;
  34. this.txt2.node.active = false;
  35. }
  36. ShowBack() {
  37. this.txt.node.active = false;
  38. this.txt2.node.active = true;
  39. }
  40. ShowBothSides() {
  41. this.txt.node.active = true;
  42. this.txt2.node.active = true;
  43. }
  44. set Text(value: string) {
  45. this.txt.string = value;
  46. this.txt2.string = value;
  47. }
  48. get Text() {
  49. return this.txt.string;
  50. }
  51. }