local NeiJiangMJDiceAnimation = class("NeiJiangMJDiceAnimation", cc.UIView); function NeiJiangMJDiceAnimation:ctor() NeiJiangMJDiceAnimation.super.ctor(self) cc.SpriteFrameCache:getInstance():addSpriteFramesWithFile("mj_neijiang/res/zy_fangjian/mj_neijiang_dice.plist"); cc.SpriteFrameCache:getInstance():addSpriteFramesWithFile("mj_neijiang/res/zy_fangjian/mj_neijiang_dice_anim.plist"); self.isPlaying = false; -- 是否正在播放动画 self.animRoot = cc.Node:create(); self:addChild(self.animRoot); end function NeiJiangMJDiceAnimation:onEnter() NeiJiangMJDiceAnimation.super.onEnter(self) end function NeiJiangMJDiceAnimation:onExit() NeiJiangMJDiceAnimation.super.onExit(self) end --[[ -- 初始化第一帧 -- @param -- @return --]] function NeiJiangMJDiceAnimation:initFirstFrame () self.animImg = cc.ImageView:createNode() self.animImg:loadTexture(string.format("mj_neijiang_dice_anim_%d.png", 0), cc.TextureResType.plistType) self.animRoot:addChild(self.animImg); end --[[ -- 播放动画 -- @param dice1 骰子1的点数 -- @param dice2 骰子2的点数 -- @return --]] function NeiJiangMJDiceAnimation:play (dice1, dice2, callback) if self.isPlaying then return ; end self.callback = callback; self.dice1 = dice1; self.dice2 = dice2; self.isPlaying = true; self:initFirstFrame(); -- 循环修改图片 local frameTime = 0.025; local currentFrame = 0; local totalFrame = 13; local maxFrame = totalFrame * 2; local action1 = cc.Sequence:create(cc.DelayTime:create(frameTime), cc.CallFunc:create(function () if currentFrame > maxFrame then self:stop(); return ; end local imgIndex = (currentFrame + 1) % totalFrame; imgIndex = imgIndex == 0 and totalFrame or imgIndex; local imgName = string.format("mj_neijiang_dice_anim_%d.png", (imgIndex - 1)) if not tolua.isnull(self.animImg) then self.animImg:loadTexture(imgName, cc.TextureResType.plistType) end currentFrame = currentFrame + 1 end)) local acRepeat = cc.Repeat:create(action1, maxFrame); local acCallFunc = cc.CallFunc:create(handler(self, self.onAnimFinished)); local acSequence = cc.Sequence:create(acRepeat, acCallFunc); self.animImg:runAction(acSequence); self.animImg:setVisible(true); end --[[ -- 停止播放动画 -- @param -- @return --]] function NeiJiangMJDiceAnimation:stop () if not self.isPlaying then return ; end self.isPlaying = false; if self.animImg then self.animImg:stopAllActions(); self.animImg:setVisible(false); end end --[[ -- 移除本身 -- @param -- @return --]] function NeiJiangMJDiceAnimation:removeSelf () self:removeFromParent(); self = nil; end --[[ -- 动画播放结束事件 -- @param -- @return --]] function NeiJiangMJDiceAnimation:onAnimFinished( ) self:stop(); self:showDiceResult(self.dice1, self.dice2); end --[[ -- 创建一颗骰子 -- @param -- @return --]] function NeiJiangMJDiceAnimation:createDice (dice) local imgName = string.format("mj_neijiang_dice_l_%d.png", dice); local imgDice = cc.ImageView:createNode(); imgDice:loadTexture(imgName, cc.TextureResType.plistType); imgDice:setScale(1.1); return imgDice; end --[[ -- 显示两个摇骰结果 -- @param -- @return --]] function NeiJiangMJDiceAnimation:showDiceResult (dice1, dice2) local imgDice1 = self:createDice(dice1); imgDice1:setPosition(cc.p(40, 70)); self.animRoot:addChild(imgDice1); local imgDice2 = self:createDice(dice2); imgDice2:setPosition(cc.p(-40, -70)); self.animRoot:addChild(imgDice2); local acDelay = cc.DelayTime:create(1.5); local acCallFunc = cc.CallFunc:create(function () self:stopAllActions(); self:removeSelf(); if self.callback and type(self.callback) == "function" then self.callback(dice1, dice2); end end); local acSequence = cc.Sequence:create(acDelay, acCallFunc); self:runAction(acSequence); end return NeiJiangMJDiceAnimation;