TweenEffect.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. const { ccclass, property, menu, executeInEditMode } = cc._decorator;
  2. export enum EaseType {
  3. none = 0,
  4. fade,
  5. easeIn,
  6. easeOut,
  7. easeInOut,
  8. easeExponentialIn,
  9. easeExponentialOut,
  10. easeExponentialInOut,
  11. easeSineIn,
  12. easeSineOut,
  13. easeSineInOut,
  14. easeElasticIn,
  15. easeElasticOut,
  16. easeElasticInOut,
  17. easeBounceIn,
  18. easeBounceOut,
  19. easeBounceInOut,
  20. easeBackIn,
  21. easeBackOut,
  22. easeBackInOut,
  23. easeBezierAction,
  24. easeQuadraticActionIn,
  25. easeQuadraticActionOut,
  26. easeQuadraticActionInOut,
  27. easeQuarticActionIn,
  28. easeQuarticActionOut,
  29. easeQuarticActionInOut,
  30. easeQuinticActionIn,
  31. easeQuinticActionOut,
  32. easeQuinticActionInOut,
  33. easeCircleActionIn,
  34. easeCircleActionOut,
  35. easeCircleActionInOut,
  36. easeCubicActionIn,
  37. easeCubicActionOut,
  38. easeCubicActionInOut
  39. }
  40. enum TweenType {
  41. none,
  42. line,
  43. angle,
  44. opacity,
  45. scale,
  46. skewY,
  47. shake,
  48. flip
  49. }
  50. enum RunTime {
  51. onLoad,
  52. start,
  53. onEnable
  54. }
  55. @ccclass
  56. @menu("Tools/TweenEffect")
  57. export default class TweenEffect extends cc.Component {
  58. @property({ type: cc.Enum(TweenType), tooltip: "动作类型" })
  59. type: TweenType = TweenType.none;
  60. @property({ type: cc.Enum(EaseType) })
  61. easeType: EaseType = EaseType.none;
  62. @property({ type: cc.Enum(RunTime), tooltip: "运行条件" })
  63. run: RunTime = RunTime.start;
  64. @property({ type: cc.Float, tooltip: "开始延迟时间" })
  65. delay: number = 0;
  66. @property({ type: cc.Float, tooltip: "单次运行时间" })
  67. time: number = 1;
  68. @property({ type: cc.Integer, tooltip: "重复次数 -1永久重复 0不重复" })
  69. repeat: number = -1;
  70. @property({ tooltip: "是否倒置" })
  71. isReverse: boolean = false;
  72. @property({ tooltip: "目标值", visible: function () { return this.type == TweenType.angle || this.type == TweenType.opacity || this.type == TweenType.scale || this.type == TweenType.skewY } })
  73. num: number = 0;
  74. @property({ tooltip: "目标坐标", visible: function () { return this.type == TweenType.line } })
  75. toPos: cc.Vec3 = cc.Vec3.ZERO;
  76. private _tw = null;
  77. protected onLoad(): void {
  78. this._tw = cc.tween(this.node);
  79. // 间隔
  80. this._tw.delay(this.delay);
  81. // 动作类型
  82. switch (this.type) {
  83. case TweenType.line: {
  84. this._line();
  85. break;
  86. }
  87. case TweenType.angle: {
  88. this._angle();
  89. break;
  90. }
  91. case TweenType.opacity: {
  92. this._opacity();
  93. break;
  94. }
  95. case TweenType.scale: {
  96. this._scale();
  97. break;
  98. }
  99. case TweenType.skewY: {
  100. this._skewY();
  101. break;
  102. }
  103. case TweenType.shake: {
  104. this._shake();
  105. break;
  106. }
  107. case TweenType.flip: {
  108. this._flip();
  109. break;
  110. }
  111. }
  112. // 循环
  113. if (this.repeat > 0) {
  114. this._tw.union().repeat(this.repeat)
  115. } else if (this.repeat == -1) {
  116. this._tw.union().repeatForever()
  117. }
  118. // 运行
  119. if (this.run == RunTime.onLoad) {
  120. this._tw.start()
  121. }
  122. }
  123. protected start(): void {
  124. if (this.run == RunTime.start) {
  125. this._tw.start()
  126. }
  127. }
  128. protected onEnable(): void {
  129. if (this.run == RunTime.onEnable) {
  130. this.node.stopAllActions();
  131. this._tw.start()
  132. }
  133. }
  134. /** 线性效果 */
  135. private _line() {
  136. this._tw.by(this.time, { position: this.toPos }, { easing: TweenEffect.getEase(this.easeType) })
  137. if (this.isReverse) this._tw.by(this.time, { position: this.toPos.neg() }, { easing: TweenEffect.getEase(this.easeType) })
  138. }
  139. /** 旋转效果 */
  140. private _angle() {
  141. this._tw.by(this.time, { angle: this.num }, { easing: TweenEffect.getEase(this.easeType) })
  142. if (this.isReverse) this._tw.by(this.time, { angle: -this.num }, { easing: TweenEffect.getEase(this.easeType) })
  143. }
  144. /** 透明效果 */
  145. private _opacity() {
  146. this._tw.by(this.time, { opacity: this.num }, { easing: TweenEffect.getEase(this.easeType) })
  147. if (this.isReverse) this._tw.by(this.time, { opacity: -this.num }, { easing: TweenEffect.getEase(this.easeType) })
  148. }
  149. /** 缩放效果 */
  150. private _scale() {
  151. this._tw.by(this.time, { scale: this.num }, { easing: TweenEffect.getEase(this.easeType) })
  152. if (this.isReverse) this._tw.by(this.time, { scale: -this.num }, { easing: TweenEffect.getEase(this.easeType) })
  153. }
  154. /** 偏斜效果 */
  155. private _skewY() {
  156. this._tw.by(this.time, { skewY: this.num }, { easing: TweenEffect.getEase(this.easeType) })
  157. if (this.isReverse) this._tw.by(this.time, { skewY: -this.num }, { easing: TweenEffect.getEase(this.easeType) })
  158. }
  159. /** 晃动效果 */
  160. private _shake() {
  161. this._tw
  162. .by(this.time, { angle: this.num }, { easing: TweenEffect.getEase(this.easeType) })
  163. .by(this.time * 2, { angle: -2 * this.num }, { easing: TweenEffect.getEase(this.easeType) })
  164. .by(this.time, { angle: this.num }, { easing: TweenEffect.getEase(this.easeType) })
  165. }
  166. /** 翻转效果 */
  167. private _flip() {
  168. this._tw
  169. .to(this.time, { scaleX: -this.node.scaleX }, { easing: TweenEffect.getEase(this.easeType) })
  170. .to(this.time, { scaleX: this.node.scaleX }, { easing: TweenEffect.getEase(this.easeType) })
  171. }
  172. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  173. //////////////////////////// 弹窗效果 //////////////////////////////////////////////////////////////////
  174. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  175. /** 遮罩渐显 */
  176. public static panel_mask_opacity(node: cc.Node, callback?: Function) {
  177. let opacityBack = node.opacity;
  178. node.opacity = 0;
  179. cc.tween(node).to(0.2, { opacity: opacityBack }).call(() => { callback && callback(); }).start();
  180. }
  181. /** 移动Y轴_打开 */
  182. public static panel_open_moveY(node: cc.Node, callback?: Function) {
  183. node.y += 1000;
  184. cc.tween(node).to(0.5, { y: node.y - 1000 }, { easing: "sineOut" }).call(() => { callback && callback(); }).start();
  185. }
  186. /** 移动Y轴_关闭 */
  187. public static panel_close_moveY(node: cc.Node, callback?: Function) {
  188. cc.tween(node).to(0.5, { y: node.y + 1000 }, { easing: "sineIn" }).call(() => { callback && callback(); }).start();
  189. }
  190. /** 缩放Y轴_打开 */
  191. public static panel_open_scaleY(node: cc.Node, callback?: Function) {
  192. node.scaleY = 0;
  193. cc.tween(node).to(0.5, { scaleY: 1 }, { easing: "sineOut" }).call(() => { callback && callback(); }).start();
  194. }
  195. /** 缩放Y轴_关闭 */
  196. public static panel_close_scaleY(node: cc.Node, callback?: Function) {
  197. cc.tween(node).to(0.5, { scaleY: 0 }, { easing: "sineIn" }).call(() => { callback && callback(); }).start();
  198. }
  199. /** 整体缩放_打开 */
  200. public static panel_open_scale(node: cc.Node, callback?: Function) {
  201. node.scale = 0;
  202. cc.tween(node).to(0.5, { scale: 1 }, { easing: "sineOut" }).call(() => { callback && callback(); }).start();
  203. }
  204. /** 整体缩放_关闭 */
  205. public static panel_close_scale(node: cc.Node, callback?: Function) {
  206. cc.tween(node).to(0.5, { scale: 0 }, { easing: "sineIn" }).call(() => { callback && callback(); }).start();
  207. }
  208. /** 透明度缩放_打开 */
  209. public static panel_open_opacity_scale(node: cc.Node, callback?: Function) {
  210. node.opacity = 0;
  211. node.scale = 1.5;
  212. cc.tween(node).to(0.5, { opacity: 255, scale: 1 }, { easing: "fade" }).call(() => { callback && callback(); }).start();
  213. }
  214. /** 透明度缩放_关闭 */
  215. public static panel_close_opacity_scale(node: cc.Node, callback?: Function) {
  216. cc.tween(node).to(0.5, { opacity: 0, scale: 2 }, { easing: "fade" }).call(() => { callback && callback(); }).start();
  217. }
  218. /** 获取ease类型 */
  219. public static getEase(type: EaseType) {
  220. switch (type) {
  221. case EaseType.none: return "linear";
  222. case EaseType.fade: return "fade";
  223. case EaseType.easeOut: return "easeOut";
  224. case EaseType.easeInOut: return "easeInOut";
  225. case EaseType.easeExponentialIn: return "easeExponentialIn";
  226. case EaseType.easeExponentialOut: return "easeExponentialOut";
  227. case EaseType.easeExponentialInOut: return "easeExponentialInOut";
  228. case EaseType.easeSineIn: return "sineIn";
  229. case EaseType.easeSineOut: return "sineOut";
  230. case EaseType.easeSineInOut: return "sineInOut";
  231. case EaseType.easeElasticIn: return "elasticIn";
  232. case EaseType.easeElasticOut: return "elasticOut";
  233. case EaseType.easeElasticInOut: return "elasticInOut";
  234. case EaseType.easeBounceIn: return "bounceIn";
  235. case EaseType.easeBounceOut: return "bounceOut";
  236. case EaseType.easeBackIn: return "backIn";
  237. case EaseType.easeBackOut: return 'backOut';
  238. case EaseType.easeBackInOut: return "backInOut";
  239. case EaseType.easeQuadraticActionIn: return "quadraticActionIn";
  240. case EaseType.easeQuadraticActionOut: return "quadraticActionOut";
  241. case EaseType.easeQuadraticActionInOut: return "quadraticActionInOut";
  242. case EaseType.easeQuarticActionIn: return "quarticActionIn";
  243. case EaseType.easeQuarticActionOut: return "quarticActionOut";
  244. case EaseType.easeQuarticActionInOut: return "quarticActionInOut";
  245. case EaseType.easeQuinticActionIn: return "quinticActionIn";
  246. case EaseType.easeQuinticActionOut: return "quinticActionOut";
  247. case EaseType.easeQuinticActionInOut: return "quinticActionInOut";
  248. case EaseType.easeCircleActionIn: return "easeCircleActionIn";
  249. case EaseType.easeCircleActionOut: return "circleActionOut";
  250. case EaseType.easeCircleActionInOut: return "circleActionInOut";
  251. case EaseType.easeCubicActionIn: return "cubicActionIn";
  252. case EaseType.easeCubicActionOut: return "cubicActionOut";
  253. case EaseType.easeCubicActionInOut: return "cubicActionInOut";
  254. }
  255. }
  256. }