You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 line
6.7 KiB

  1. --[[
  2. 金币动画播放器
  3. 使用方法:
  4. local coinPlayer = import("luaScript.Tools.CoinAniPlayer"):new()
  5. if coinPlayer then
  6. local nodeBegin = self.ui.Items.Button_AddDiamand
  7. local nodeEnd = self.ui.Items.Button_KeFu
  8. local goldNum = 100
  9. local goldIcon = "jinbi.png"
  10. local endCallback = function()
  11. showTooltip("onEnd")
  12. end
  13. local sizeRange = cc.size(50, 50)
  14. coinPlayer:playAnimation(nodeBegin, nodeEnd, goldNum, goldIcon, endCallback, sizeRange);
  15. end
  16. --]]
  17. local CoinAniPlayer = class("CoinAniPlayer")
  18. function CoinAniPlayer:ctor(goldIcon)
  19. self.goldIcon = goldIcon
  20. --初始化金币池
  21. self:initCoinPool()
  22. end
  23. --[[
  24. 金币对象池
  25. ]]
  26. function CoinAniPlayer:initCoinPool()
  27. local maxCoin = 200
  28. --金币缓冲池
  29. self.coinList = {}
  30. --金币激活池
  31. --self.coinActiveList = {}
  32. --延迟加载创建金币
  33. for i = 1 ,maxCoin do
  34. local seq = cc.Sequence:create(cc.DelayTime:create(i * 0.01),cc.CallFunc:create(function ()
  35. local outCoin = cc.ImageView:createNode()
  36. outCoin:loadTextureFromPlist(self.goldIcon)
  37. app.mainScene:addChild(outCoin)
  38. local randomX = math.random(-34,34)
  39. local randomY = math.random(-34,34)
  40. outCoin:setPosition(cc.p(640+ randomX,360+ randomY))
  41. outCoin:setVisible(false)
  42. outCoin:retain()
  43. table.insert(self.coinList,outCoin)
  44. if i == maxCoin then
  45. --showTooltip("初始化金币池完成")
  46. end
  47. end))
  48. app.mainScene:runAction(seq)
  49. end
  50. end
  51. --取出一个金币
  52. function CoinAniPlayer:takeOutCoin()
  53. local object
  54. if table.nums(self.coinList) > 0 then
  55. object = self.coinList[1]
  56. table.remove(self.coinList,1)
  57. else
  58. --立马创建
  59. for i = 1 ,60 do
  60. local outCoin = cc.ImageView:createNode()
  61. outCoin:loadTextureFromPlist(self.goldIcon)
  62. app.mainScene:addChild(outCoin)
  63. local randomX = math.random(-34,34)
  64. local randomY = math.random(-34,34)
  65. outCoin:setPosition(cc.p(640+ randomX,360+ randomY))
  66. outCoin:setVisible(false)
  67. outCoin:retain()
  68. table.insert(self.coinList,outCoin)
  69. end
  70. object = self.coinList[1]
  71. table.remove(self.coinList,1)
  72. print("对象池不够了,创建新的金币")
  73. --showTooltip("对象池不够了,创建新的金币")
  74. end
  75. return object
  76. end
  77. --放回一个金币
  78. function CoinAniPlayer:pushCoinPool(coin)
  79. --coin:release()
  80. --重置精灵的状态
  81. coin:setVisible(false)
  82. coin:setOpacity(255)
  83. --坐标可以设置或者不设置都可以
  84. local randomX = math.random(-34,34)
  85. local randomY = math.random(-34,34)
  86. coin:setPosition(cc.p(640+ randomX,360+ randomY))
  87. table.insert(self.coinList,coin)
  88. --logD("pushCoinPool num:"..table.nums(self.coinList))
  89. end
  90. --[[
  91. 金币动画
  92. nodeBegin : 起始点
  93. nodeEnd : 结束点
  94. goldNum : 金币数量
  95. goldIcon : 金币图标
  96. endCallback : 动画完成后的回调
  97. sizeRange : 范围大小
  98. --]]
  99. function CoinAniPlayer:playAnimation(nodeBegin, nodeEnd, goldNum, voiceName,endCallback, sizeRange)
  100. local posBegin = nodeBegin:getWorldPosition()
  101. local posEnd = nodeEnd:getWorldPosition()
  102. local goldNum = goldNum or 0
  103. if sizeRange then
  104. self:_playAnimationR2R(posBegin, posEnd, goldNum, voiceName,endCallback, sizeRange)
  105. else
  106. self:_playAnimationP2P(posBegin, posEnd, goldNum, voiceName,endCallback)
  107. end
  108. end
  109. --[[
  110. 金币动画点到点
  111. nodeBegin : 起始点
  112. nodeEnd : 结束点
  113. goldNum : 金币数量
  114. goldIcon : 金币图标 - 从SpriteFrameCacha中读取
  115. endCallback : 动画完成后的回调
  116. --]]
  117. function CoinAniPlayer:_playAnimationP2P(posBegin, posEnd, goldNum,voiceName,endCallback)
  118. --local moveTime = 0.6
  119. --local moveTime = 0.45
  120. local moveTime = 0.47
  121. local intrval = 0.08
  122. local coinMax = 25
  123. if goldNum == 24 then
  124. goldNum = 18
  125. elseif goldNum == 12 then
  126. goldNum = 8
  127. end
  128. if goldNum > 8 then
  129. intrval = 0.06
  130. end
  131. if goldNum > coinMax then
  132. goldNum = 22
  133. end
  134. local numCoin = self:_getCoinNum(goldNum)
  135. voiceName = voiceName or "res/sound/room/ph_coin.ogg"
  136. for i = 1, numCoin do
  137. local spCoin = self:takeOutCoin()
  138. if spCoin then
  139. -- 设置起始位置
  140. spCoin:setVisible(true)
  141. spCoin:setAnchorPoint(cc.p(0.5, 0.5))
  142. spCoin:setPosition(posBegin)
  143. -- 延迟 - 淡入 - 移动 - 回调
  144. local function onPlayEnd()
  145. -- 丢回对象池
  146. self:pushCoinPool(spCoin)
  147. -- 如果是最后一个金币播放完成,则通知回调
  148. if i == numCoin then
  149. if endCallback then
  150. endCallback()
  151. end
  152. end
  153. end
  154. local spawn = cc.Spawn:create(
  155. cc.MoveTo:create(moveTime, cc.p(posEnd.x, posEnd.y)),
  156. cc.ScaleTo:create(moveTime, 1.0),cc.CallFunc:create(function()
  157. playVoice(voiceName)
  158. end));
  159. local seq = cc.Sequence:create(
  160. cc.DelayTime:create(i * intrval),
  161. cc.FadeIn:create(0.01),
  162. cc.EaseSineOut:create(spawn),
  163. --spawn,
  164. cc.CallFunc:create(onPlayEnd));
  165. spCoin:runAction(seq)
  166. end
  167. end
  168. end
  169. --[[
  170. 金币动画范围到范围
  171. nodeBegin : 起始点
  172. nodeEnd : 结束点
  173. goldNum : 金币数量
  174. goldIcon : 金币图标
  175. endCallback : 动画完成后的回调
  176. sizeRange : 范围大小
  177. --]]
  178. function CoinAniPlayer:_playAnimationR2R(posBegin, posEnd, goldNum, voiceName ,endCallback, sizeRange)
  179. local moveTime = 1.0
  180. local numCoin = self:_getCoinNum(goldNum)
  181. voiceName = voiceName or "res/sound/room/ph_coin.ogg"
  182. for i = 1, numCoin do
  183. local spCoin = self:takeOutCoin()
  184. if spCoin then
  185. spCoin:setVisible(true)
  186. -- 随机起始点
  187. local randomX = math.random(-sizeRange.width / 2, sizeRange.width / 2)
  188. local randomY = math.random(-sizeRange.height / 2, sizeRange.height / 2)
  189. local posBegin = cc.p(posBegin.x + randomX, posBegin.y + randomY )
  190. -- 随机结束点
  191. local randomX = math.random(-sizeRange.width / 2, sizeRange.width / 2)
  192. local randomY = math.random(-sizeRange.height / 2, sizeRange.height / 2)
  193. local posEnd = cc.p(posEnd.x + randomX, posEnd.y + randomY )
  194. -- 设置起始位置
  195. spCoin:setAnchorPoint(cc.p(0.5, 0.5))
  196. spCoin:setPosition(posBegin)
  197. -- 延迟 - 淡入 - 移动 - 回调
  198. local function onPlayEnd()
  199. -- 丢回对象池
  200. self:pushCoinPool(spCoin)
  201. -- 如果是最后一个金币播放完成,则通知回调
  202. if i == numCoin then
  203. if endCallback then
  204. endCallback()
  205. end
  206. end
  207. end
  208. local spawn = cc.Spawn:create(
  209. cc.MoveTo:create(moveTime, cc.p(posEnd.x, posEnd.y)),
  210. cc.ScaleTo:create(moveTime, 1.0),cc.CallFunc:create(function()
  211. playVoice(voiceName)
  212. end));
  213. local seq = cc.Sequence:create(
  214. cc.DelayTime:create(i * 0.01),
  215. cc.FadeIn:create(0.01),
  216. cc.EaseSineOut:create(spawn),
  217. cc.CallFunc:create(onPlayEnd));
  218. spCoin:runAction(seq)
  219. end
  220. end
  221. end
  222. -- 获取需要创建的金币图标的个数
  223. function CoinAniPlayer:_getCoinNum(num)
  224. if num then
  225. return num
  226. else
  227. return 10
  228. end
  229. end
  230. return CoinAniPlayer