EffectOpenAndClose.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. const { ccclass, property, executeInEditMode, playOnFocus } = cc._decorator;
  2. /** 效果类型 */
  3. enum EffectType {
  4. none = 0, //无
  5. scale = 1, //缩放
  6. scaleToPoint = 2, //缩放到某点
  7. centerUnfolding = 3, //从中部展开
  8. rightIn, //从右侧进入
  9. }
  10. /**
  11. * 界面打开 和 关闭效果
  12. * 组件请挂载到对应ui界面上
  13. * @author 薛鸿潇
  14. */
  15. @ccclass
  16. @executeInEditMode()
  17. @playOnFocus()
  18. export default class EffectOpenAndClose extends cc.Component {
  19. @property({ type: cc.Node, displayName: '动画节点' })
  20. private effect_par: cc.Node = null
  21. @property({ type: cc.Enum(EffectType), displayName: '打开动画节点效果', visible() { return this.effect_par } })
  22. private open_effect: EffectType = EffectType.scale;
  23. @property({ type: cc.Enum(EffectType), displayName: '关闭动画节点效果', visible() { return this.effect_par } })
  24. private close_effect: EffectType = EffectType.scale;
  25. @property({ displayName: '关闭节点缩小到此节点坐标', visible() { return this.close_effect == EffectType.scaleToPoint } })
  26. private close_toPoint: string = "";
  27. private _play_open: boolean = false;
  28. @property({ displayName: "预览开" })
  29. get play_open(): boolean {
  30. if (this._play_open) {
  31. this.showEffect();
  32. this._play_open = false;
  33. }
  34. return this._play_open;
  35. }
  36. set play_open(v: boolean) {
  37. this._play_open = v;
  38. }
  39. private _play_close: boolean = false;
  40. @property({ displayName: "预览关" })
  41. get play_close(): boolean {
  42. if (this._play_close) {
  43. this.effect_par.scale = 1;
  44. this.hideEffect();
  45. this._play_close = false;
  46. }
  47. return this._play_close;
  48. }
  49. set play_close(v: boolean) {
  50. this._play_close = v;
  51. }
  52. onLoad() {
  53. // this.node.on('ui-close', this.hideEffect, this);// 界面关闭事件
  54. }
  55. start() {
  56. if (CC_EDITOR) return;
  57. this.showEffect();
  58. }
  59. /**
  60. * 界面打开效果
  61. */
  62. private showEffect() {
  63. if (this.effect_par) {
  64. switch (this.open_effect) {
  65. case EffectType.none:
  66. break;
  67. case EffectType.scale:
  68. this.effect_par.scale = 0;
  69. cc.tween(this.effect_par)
  70. .to(0.15, { scale: 1 })
  71. .start()
  72. break;
  73. case EffectType.centerUnfolding:
  74. this.effect_par.scaleX = 0;
  75. cc.tween(this.effect_par)
  76. .to(0.15, { scaleX: 1 })
  77. .start()
  78. break;
  79. case EffectType.rightIn:
  80. this.effect_par.active = true;
  81. this.effect_par.x = 1000;
  82. cc.tween(this.effect_par)
  83. .to(0.3, { x: 0 }, cc.easeOut(3))
  84. .start()
  85. break;
  86. }
  87. }
  88. }
  89. /**
  90. * 关闭打开效果
  91. * @param cb 结束回调
  92. */
  93. public hideEffect(cb: Function = null) {
  94. if (this.effect_par) {
  95. switch (this.close_effect) {
  96. case EffectType.none:
  97. cb && cb();
  98. break;
  99. case EffectType.scale:
  100. cc.tween(this.effect_par)
  101. .to(0.15, { scale: 0 })
  102. .call(() => {
  103. cb && cb();
  104. })
  105. .start()
  106. break;
  107. case EffectType.scaleToPoint:
  108. if (this.close_toPoint == "") {
  109. console.error('需要配置缩小到终点节点路径')
  110. } else {
  111. let path = this.close_toPoint;
  112. let node = cc.find(path);
  113. if (node) {
  114. let world_pos = node.parent.convertToWorldSpaceAR(node.getPosition());
  115. let node_pos = this.effect_par.parent.convertToNodeSpaceAR(world_pos);
  116. cc.tween(this.effect_par)
  117. .to(0.3, { scale: 0, position: new cc.Vec3(node_pos.x, node_pos.y, 0) })
  118. .call(() => {
  119. cb && cb();
  120. })
  121. .start()
  122. } else {
  123. cb && cb();
  124. cc.error('关闭时动画节点未找到,请检查!!!!!')
  125. }
  126. }
  127. break;
  128. case EffectType.rightIn:
  129. cc.tween(this.effect_par)
  130. .to(0.3, { x: 1000 }, cc.easeOut(3))
  131. .call(() => {
  132. this.effect_par.active = false;
  133. cb && cb();
  134. })
  135. .start()
  136. break;
  137. }
  138. } else {
  139. cb && cb();
  140. }
  141. }
  142. }