|
- --[[
- 金币动画播放器
-
- 使用方法:
- local coinPlayer = import("luaScript.Tools.CoinAniPlayer"):new()
- if coinPlayer then
- local nodeBegin = self.ui.Items.Button_AddDiamand
- local nodeEnd = self.ui.Items.Button_KeFu
- local goldNum = 100
- local goldIcon = "jinbi.png"
- local endCallback = function()
- showTooltip("onEnd")
- end
- local sizeRange = cc.size(50, 50)
- coinPlayer:playAnimation(nodeBegin, nodeEnd, goldNum, goldIcon, endCallback, sizeRange);
- end
-
- --]]
- local CoinAniPlayer = class("CoinAniPlayer")
-
- function CoinAniPlayer:ctor(goldIcon)
- self.goldIcon = goldIcon
- --初始化金币池
- self:initCoinPool()
- end
-
-
- --[[
- 金币对象池
- ]]
- function CoinAniPlayer:initCoinPool()
- local maxCoin = 200
- --金币缓冲池
- self.coinList = {}
- --金币激活池
- --self.coinActiveList = {}
-
- --延迟加载创建金币
- for i = 1 ,maxCoin do
- local seq = cc.Sequence:create(cc.DelayTime:create(i * 0.01),cc.CallFunc:create(function ()
- local outCoin = cc.ImageView:createNode()
- outCoin:loadTextureFromPlist(self.goldIcon)
- app.mainScene:addChild(outCoin)
- local randomX = math.random(-34,34)
- local randomY = math.random(-34,34)
- outCoin:setPosition(cc.p(640+ randomX,360+ randomY))
- outCoin:setVisible(false)
- outCoin:retain()
- table.insert(self.coinList,outCoin)
- if i == maxCoin then
- --showTooltip("初始化金币池完成")
- end
- end))
- app.mainScene:runAction(seq)
- end
- end
-
-
- --取出一个金币
- function CoinAniPlayer:takeOutCoin()
- local object
- if table.nums(self.coinList) > 0 then
- object = self.coinList[1]
- table.remove(self.coinList,1)
- else
- --立马创建
- for i = 1 ,60 do
- local outCoin = cc.ImageView:createNode()
- outCoin:loadTextureFromPlist(self.goldIcon)
- app.mainScene:addChild(outCoin)
- local randomX = math.random(-34,34)
- local randomY = math.random(-34,34)
- outCoin:setPosition(cc.p(640+ randomX,360+ randomY))
- outCoin:setVisible(false)
- outCoin:retain()
- table.insert(self.coinList,outCoin)
- end
- object = self.coinList[1]
- table.remove(self.coinList,1)
- print("对象池不够了,创建新的金币")
- --showTooltip("对象池不够了,创建新的金币")
- end
-
- return object
- end
-
-
- --放回一个金币
- function CoinAniPlayer:pushCoinPool(coin)
- --coin:release()
- --重置精灵的状态
- coin:setVisible(false)
- coin:setOpacity(255)
- --坐标可以设置或者不设置都可以
- local randomX = math.random(-34,34)
- local randomY = math.random(-34,34)
- coin:setPosition(cc.p(640+ randomX,360+ randomY))
- table.insert(self.coinList,coin)
- --logD("pushCoinPool num:"..table.nums(self.coinList))
- end
-
- --[[
- 金币动画
- nodeBegin : 起始点
- nodeEnd : 结束点
- goldNum : 金币数量
- goldIcon : 金币图标
- endCallback : 动画完成后的回调
- sizeRange : 范围大小
- --]]
- function CoinAniPlayer:playAnimation(nodeBegin, nodeEnd, goldNum, voiceName,endCallback, sizeRange)
-
- local posBegin = nodeBegin:getWorldPosition()
- local posEnd = nodeEnd:getWorldPosition()
- local goldNum = goldNum or 0
-
-
- if sizeRange then
- self:_playAnimationR2R(posBegin, posEnd, goldNum, voiceName,endCallback, sizeRange)
- else
- self:_playAnimationP2P(posBegin, posEnd, goldNum, voiceName,endCallback)
- end
- end
-
- --[[
- 金币动画点到点
- nodeBegin : 起始点
- nodeEnd : 结束点
- goldNum : 金币数量
- goldIcon : 金币图标 - 从SpriteFrameCacha中读取
- endCallback : 动画完成后的回调
- --]]
- function CoinAniPlayer:_playAnimationP2P(posBegin, posEnd, goldNum,voiceName,endCallback)
- --local moveTime = 0.6
- --local moveTime = 0.45
- local moveTime = 0.47
- local intrval = 0.08
- local coinMax = 25
- if goldNum == 24 then
- goldNum = 18
- elseif goldNum == 12 then
- goldNum = 8
- end
- if goldNum > 8 then
- intrval = 0.06
- end
- if goldNum > coinMax then
- goldNum = 22
- end
-
- local numCoin = self:_getCoinNum(goldNum)
- voiceName = voiceName or "res/sound/room/ph_coin.ogg"
- for i = 1, numCoin do
- local spCoin = self:takeOutCoin()
- if spCoin then
- -- 设置起始位置
- spCoin:setVisible(true)
- spCoin:setAnchorPoint(cc.p(0.5, 0.5))
- spCoin:setPosition(posBegin)
-
- -- 延迟 - 淡入 - 移动 - 回调
- local function onPlayEnd()
- -- 丢回对象池
- self:pushCoinPool(spCoin)
- -- 如果是最后一个金币播放完成,则通知回调
- if i == numCoin then
- if endCallback then
- endCallback()
- end
- end
- end
- local spawn = cc.Spawn:create(
- cc.MoveTo:create(moveTime, cc.p(posEnd.x, posEnd.y)),
- cc.ScaleTo:create(moveTime, 1.0),cc.CallFunc:create(function()
- playVoice(voiceName)
- end));
- local seq = cc.Sequence:create(
- cc.DelayTime:create(i * intrval),
- cc.FadeIn:create(0.01),
- cc.EaseSineOut:create(spawn),
- --spawn,
- cc.CallFunc:create(onPlayEnd));
- spCoin:runAction(seq)
- end
- end
- end
-
- --[[
- 金币动画范围到范围
- nodeBegin : 起始点
- nodeEnd : 结束点
- goldNum : 金币数量
- goldIcon : 金币图标
- endCallback : 动画完成后的回调
- sizeRange : 范围大小
- --]]
- function CoinAniPlayer:_playAnimationR2R(posBegin, posEnd, goldNum, voiceName ,endCallback, sizeRange)
- local moveTime = 1.0
- local numCoin = self:_getCoinNum(goldNum)
- voiceName = voiceName or "res/sound/room/ph_coin.ogg"
- for i = 1, numCoin do
- local spCoin = self:takeOutCoin()
- if spCoin then
- spCoin:setVisible(true)
- -- 随机起始点
- local randomX = math.random(-sizeRange.width / 2, sizeRange.width / 2)
- local randomY = math.random(-sizeRange.height / 2, sizeRange.height / 2)
- local posBegin = cc.p(posBegin.x + randomX, posBegin.y + randomY )
-
- -- 随机结束点
- local randomX = math.random(-sizeRange.width / 2, sizeRange.width / 2)
- local randomY = math.random(-sizeRange.height / 2, sizeRange.height / 2)
- local posEnd = cc.p(posEnd.x + randomX, posEnd.y + randomY )
-
- -- 设置起始位置
- spCoin:setAnchorPoint(cc.p(0.5, 0.5))
- spCoin:setPosition(posBegin)
-
- -- 延迟 - 淡入 - 移动 - 回调
- local function onPlayEnd()
- -- 丢回对象池
- self:pushCoinPool(spCoin)
- -- 如果是最后一个金币播放完成,则通知回调
- if i == numCoin then
- if endCallback then
- endCallback()
- end
- end
- end
- local spawn = cc.Spawn:create(
- cc.MoveTo:create(moveTime, cc.p(posEnd.x, posEnd.y)),
- cc.ScaleTo:create(moveTime, 1.0),cc.CallFunc:create(function()
- playVoice(voiceName)
- end));
- local seq = cc.Sequence:create(
- cc.DelayTime:create(i * 0.01),
- cc.FadeIn:create(0.01),
- cc.EaseSineOut:create(spawn),
- cc.CallFunc:create(onPlayEnd));
- spCoin:runAction(seq)
- end
- end
- end
-
- -- 获取需要创建的金币图标的个数
- function CoinAniPlayer:_getCoinNum(num)
- if num then
- return num
- else
- return 10
- end
- end
-
- return CoinAniPlayer
|