Cube_Infor.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. let rotationY = this.node.eulerAngles.y;
  25. let rotationZ = this.node.eulerAngles.z;
  26. if (rotationX > -160 && rotationX < -30) {
  27. this.ShowFront(); // 显示正面字
  28. } else if(rotationX > 30 && rotationX <= 160) {
  29. this.ShowBack(); // 显示反面字
  30. }else {
  31. this.ShowBothSides();
  32. }
  33. }
  34. ShowFront() {
  35. this.txt.node.active = true;
  36. this.txt2.node.active = false;
  37. }
  38. ShowBack() {
  39. this.txt.node.active = false;
  40. this.txt2.node.active = true;
  41. }
  42. ShowBothSides() {
  43. this.txt.node.active = true;
  44. this.txt2.node.active = true;
  45. }
  46. set Text(value: string) {
  47. this.txt.string = value;
  48. this.txt2.string = value;
  49. }
  50. get Text() {
  51. return this.txt.string;
  52. }
  53. }