MouseSpin.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
  7. const { ccclass, property } = cc._decorator;
  8. @ccclass
  9. export default class MouseSpin extends cc.Component {
  10. @property(sp.Skeleton)
  11. spinLizi: sp.Skeleton = null;
  12. @property(sp.Skeleton)
  13. spinLight: sp.Skeleton = null;
  14. @property(cc.Sprite)
  15. spinRotate: cc.Sprite = null;
  16. @property(cc.Node)
  17. spinHover: cc.Node = null;
  18. @property([cc.SpriteFrame])
  19. spinRotateImgs: cc.SpriteFrame[] = [];
  20. private rotateSpeed: number = 110;
  21. private isSpining = false;
  22. private needRotate = true;
  23. private isDark = false;
  24. protected onLoad(): void {
  25. this.node.on(cc.Node.EventType.MOUSE_ENTER, () => {
  26. if (this.isSpining)
  27. return;
  28. this.spinHover.active = true;
  29. }, this);
  30. this.node.on(cc.Node.EventType.MOUSE_LEAVE, () => {
  31. if (this.isSpining)
  32. return;
  33. this.spinHover.active = false;
  34. });
  35. }
  36. spinStart() {
  37. this.isSpining = true;
  38. this.spinHover.active = false;
  39. this.spinLizi.node.active = true;
  40. this.spinLizi.setAnimation(0, "btn", false);
  41. this.spinLizi.setCompleteListener(() => {
  42. this.spinLizi.setCompleteListener(null);
  43. this.spinLizi.node.active = false;
  44. });
  45. this.spinLight.node.active = true;
  46. this.spinLight.setAnimation(0, "btn", false);
  47. this.spinLight.setCompleteListener(() => {
  48. this.spinLight.setCompleteListener(null);
  49. this.spinLight.node.active = false;
  50. });
  51. this.rotateSpeed = 800;
  52. cc.tween(this.node)
  53. .to(0.1, { scale: 0.9 })
  54. .to(0.1, { scale: 1 })
  55. .start();
  56. }
  57. spinPause() {
  58. this.spinDark();
  59. this.needRotate = false;
  60. }
  61. spinDark() {
  62. if (this.isDark)
  63. return;
  64. cc.error("========> spinDark");
  65. this.isDark = true;
  66. this.spinRotate.spriteFrame = this.spinRotateImgs[1];
  67. }
  68. spinOver() {
  69. this.spinRotate.spriteFrame = this.spinRotateImgs[0];
  70. this.isSpining = false;
  71. this.rotateSpeed = 110;
  72. this.needRotate = true;
  73. this.isDark = false;
  74. // this.spinSke.setAnimation(0, "btn", true);
  75. this.spinLizi.node.active = false;
  76. this.spinLight.node.active = false;
  77. }
  78. protected update(dt: number): void {
  79. if (!this.needRotate)
  80. return;
  81. if (this.spinHover.active) {
  82. this.spinHover.angle -= this.rotateSpeed * dt;
  83. }
  84. this.spinRotate.node.angle -= this.rotateSpeed * dt;
  85. }
  86. }