TweenUtil.ts 3.5 KB

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