Shake.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import { _decorator, Component, Node, Tween, tween } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('Misc/Shake')
  4. export class Shake extends Component {
  5. @property
  6. private shakeAngle: number = 15
  7. @property
  8. private shakeInterval: number = 0.1
  9. @property
  10. private delay: number = 2
  11. private tw: Tween<Node> = null
  12. protected onEnable(): void {
  13. this.tw = tween(this.node)
  14. this.tw.sequence(
  15. tween(this.node).delay(this.delay),
  16. tween(this.node).to(this.shakeInterval, { angle: this.shakeAngle }),
  17. tween(this.node).to(this.shakeInterval, { angle: -this.shakeAngle }),
  18. tween(this.node).to(this.shakeInterval, { angle: this.shakeAngle }),
  19. tween(this.node).to(this.shakeInterval, { angle: -this.shakeAngle }),
  20. tween(this.node).to(this.shakeInterval, { angle: 0 }),
  21. ).repeatForever().start()
  22. }
  23. protected onDisable(): void {
  24. this.node.angle = 0
  25. this.tw.stop()
  26. this.tw = null
  27. }
  28. }