TweenUtil.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. const { ccclass, property } = cc._decorator;
  2. /**
  3. * 动画工具类
  4. * @author 薛鸿潇
  5. */
  6. @ccclass
  7. export default class TweenUtil {
  8. /**
  9. * 缓动类型
  10. */
  11. public easing: typeof TweenEasing = TweenEasing
  12. /**
  13. * 缩放效果
  14. * @param node 动画节点
  15. * @param time 动画时间
  16. * @param start_scale 起始缩放属性
  17. * @param end_scale 目标缩放属性
  18. * @param complete_fun 完成回调
  19. */
  20. public scale(node: cc.Node, time: number = 0.15, start_scale: number = 0, end_scale: number = 1, complete_fun?: Function, easing?: keyof typeof TweenEasing) {
  21. node.scale = start_scale;
  22. let scale1 = cc.tween().to(time, { scale: end_scale }, { easing: easing });
  23. let call1 = cc.tween().call(() => { if (typeof complete_fun == 'function') complete_fun(); })
  24. cc.tween(node).then(scale1).then(call1).start()
  25. }
  26. /**
  27. * 缩放效果
  28. * @param node 动画节点
  29. * @param time 动画时间
  30. * @param start_scale 起始缩放属性
  31. * @param end_scale 目标缩放属性
  32. * @param complete_fun 完成回调
  33. */
  34. public scaleX(node: cc.Node, time: number = 0.15, start_scale: number = 0, end_scale: number = 1, complete_fun?: Function, easing?: keyof typeof TweenEasing) {
  35. node.scaleX = start_scale;
  36. let scale1 = cc.tween().to(time, { scaleX: end_scale }, { easing: easing });
  37. let call1 = cc.tween().call(() => { if (typeof complete_fun == 'function') complete_fun(); })
  38. cc.tween(node).then(scale1).then(call1).start()
  39. }
  40. /**
  41. * 缩放移动效果
  42. * @param node 动画节点
  43. * @param time 动画时间
  44. * @param start_scale 起始缩放属性
  45. * @param end_scale 结束缩放属性
  46. * @param end_pos 结束节点位置
  47. * @param complete_fun 完成回调
  48. */
  49. public scaleParallelMove(node: cc.Node, time: number = 0.15, start_scale: number = 0, end_scale: number = 1, end_pos: cc.Vec2 = cc.Vec2.ZERO, complete_fun?: Function) {
  50. node.scale = start_scale;
  51. let scale1 = cc.tween().to(time, { scale: end_scale });
  52. let pos1 = cc.tween().to(time, { position: new cc.Vec3(end_pos.x, end_pos.y, 0) });
  53. let call1 = cc.tween().call(() => { if (typeof complete_fun == 'function') complete_fun(); })
  54. let parallel = cc.tween().parallel(scale1, pos1)
  55. cc.tween(node).then(parallel).then(call1).start();
  56. }
  57. /**
  58. * 移动效果
  59. * @param node
  60. * @param time
  61. * @param complete_fun
  62. */
  63. public move(node: cc.Node, time: number = 0.15, start_pos: cc.Vec2 = cc.Vec2.ZERO, end_pos: cc.Vec2 = cc.Vec2.ZERO, complete_fun?: Function, easing?: keyof typeof TweenEasing) {
  64. node.setPosition(start_pos);
  65. let pos1 = cc.tween().to(time, { position: new cc.Vec3(end_pos.x, end_pos.y, 0) }, { easing: easing });// cc.easeOut(3)
  66. let call1 = cc.tween().call(() => { if (typeof complete_fun == 'function') complete_fun(); })
  67. cc.tween(node).then(pos1).then(call1).start();
  68. }
  69. }
  70. /**
  71. * 缓动类型
  72. */
  73. enum TweenEasing {
  74. linear,
  75. smooth,
  76. fade,
  77. quadIn,
  78. quadOut,
  79. quadInOut,
  80. quadOutIn,
  81. cubicIn,
  82. cubicOut,
  83. cubicInOut,
  84. cubicOutIn,
  85. quartIn,
  86. quartOut,
  87. quartInOut,
  88. quartOutIn,
  89. quintIn,
  90. quintOut,
  91. quintInOut,
  92. quintOutIn,
  93. sineIn,
  94. sineOut,
  95. sineInOut,
  96. sineOutIn,
  97. expoIn,
  98. expoOut,
  99. expoInOut,
  100. expoOutIn,
  101. circIn,
  102. circOut,
  103. circInOut,
  104. circOutIn,
  105. elasticIn,
  106. elasticOut,
  107. elasticInOut,
  108. elasticOutIn,
  109. backIn,
  110. backOut,
  111. backInOut,
  112. backOutIn,
  113. bounceIn,
  114. bounceOut,
  115. bounceInOut,
  116. bounceOutIn
  117. }