RopeSwung.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. const { ccclass, property, executeInEditMode, playOnFocus } = cc._decorator;
  2. /**
  3. * 绳索摆动效果
  4. * @author 薛鸿潇
  5. */
  6. @ccclass
  7. @executeInEditMode()
  8. @playOnFocus()
  9. export default class RopeSwung extends cc.Component {
  10. @property({ displayName: '摆动角度' })
  11. private angle: number = 15;
  12. @property({ displayName: '摆动时间', tooltip: '摆动1/4距离消耗的时间' })
  13. private time: number = 1;
  14. /** 编辑器预览 */
  15. private _edit_play: boolean = false;
  16. @property({ displayName: '编辑器预览' })
  17. get edit_play(): boolean {
  18. if (this._edit_play && !this.edit_playing) this.initShakeSlow();
  19. this.edit_playing = this._edit_play;
  20. return this._edit_play;
  21. }
  22. set edit_play(v: boolean) {
  23. this._edit_play = v;
  24. if (!v) this.stop();
  25. }
  26. /** 编辑器是否正在播放 */
  27. private edit_playing = false;
  28. onLoad() {
  29. }
  30. start() {
  31. if (CC_EDITOR) {
  32. if (this.edit_play) {
  33. this.initShakeSlow();
  34. }
  35. } else {
  36. this.initShakeSlow();
  37. }
  38. }
  39. /**
  40. * 缓慢摇动
  41. * @param node 动画节点
  42. */
  43. private initShakeSlow() {
  44. let node = this.node;
  45. let rotate1 = cc.tween().to(this.time, { angle: { value: this.angle, easing: 'sineOut' } });
  46. let rotate2 = cc.tween().to(this.time, { angle: { value: 0, easing: 'sineIn' } });
  47. let rotate3 = cc.tween().to(this.time, { angle: { value: -this.angle, easing: 'sineOut' } });
  48. let rotate4 = cc.tween().to(this.time, { angle: { value: 0, easing: 'sineIn' } });
  49. let sequence1 = cc.tween().sequence(rotate1, rotate2, rotate3, rotate4);
  50. cc.tween(node).then(sequence1).repeatForever().start();
  51. }
  52. /** 停止动画 */
  53. private stop() {
  54. this.node.stopAllActions();
  55. }
  56. }