ZPathFalling.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. const { ccclass, property, executeInEditMode, playOnFocus } = cc._decorator;
  2. /**
  3. * Z字飘落效果
  4. * @author 薛鸿潇
  5. */
  6. @ccclass
  7. @executeInEditMode()
  8. @playOnFocus()
  9. export default class ZPathFalling extends cc.Component {
  10. @property({ displayName: '自动播放' }) private play_on_load: boolean = true;
  11. @property({ displayName: '完成回调', type: cc.Component.EventHandler }) public onComplete: cc.Component.EventHandler = new cc.Component.EventHandler();
  12. /** 编辑器预览 */
  13. private _edit_play: boolean = false;
  14. @property({ displayName: '编辑器预览' })
  15. get edit_play(): boolean {
  16. if (this._edit_play && !this.edit_playing) {
  17. this.edit_playing = true;
  18. this.play();
  19. }
  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. this.edit_play && this.play();
  33. } else {
  34. this.play_on_load && this.play();
  35. }
  36. }
  37. /** z字落地术 */
  38. private play() {
  39. // z字三点式
  40. let end_pos1 = this.node.getPosition();
  41. let end_pos2 = this.node.getPosition();
  42. let end_pos3 = this.node.getPosition();
  43. // 左右坐标
  44. if (this.node.x >= 0) {
  45. // 在右边,左飘
  46. end_pos1.x -= this.node.width * 3;
  47. end_pos2.x -= this.node.width * 1;
  48. end_pos3.x -= this.node.width * 2;
  49. } else {
  50. // 在左边,右飘
  51. end_pos1.x += this.node.width * 3;
  52. end_pos2.x += this.node.width * 1;
  53. end_pos3.x += this.node.width * 2;
  54. }
  55. // 下落
  56. end_pos1.y -= this.node.height / 2;
  57. end_pos2.y -= this.node.height;
  58. end_pos3.y -= this.node.height * 1.2;
  59. // // 上下坐标
  60. // if (this.node.y <= -200) {
  61. // // 在下面
  62. // end_pos1.y += this.node.height / 1;
  63. // end_pos2.y += this.node.height * 1.5;
  64. // end_pos3.y += this.node.height * 2;
  65. // } else {
  66. // // 在上面
  67. // end_pos1.y -= this.node.height / 2;
  68. // end_pos2.y -= this.node.height;
  69. // end_pos3.y -= this.node.height * 1.2;
  70. // }
  71. let pos1 = cc.tween().to(0.5, { position: end_pos1 }, { easing: 'cubicOut' })
  72. let pos2 = cc.tween().to(0.5, { position: end_pos2 }, { easing: 'cubicOut' })
  73. let pos3 = cc.tween().to(0.5, { position: end_pos3 }, { easing: 'cubicOut' })
  74. let call1 = cc.tween().call(() => {
  75. this.edit_playing = false;
  76. this.edit_play = false;
  77. this.onComplete.handler && this.onComplete.emit([this.node]);
  78. })
  79. cc.tween(this.node).then(pos1).then(pos2).then(pos3).then(call1).start();
  80. }
  81. /** 停止动画 */
  82. private stop() {
  83. this.edit_playing = false;
  84. this.node.stopAllActions();
  85. }
  86. // update (dt) {}
  87. }