|
- local MJDiceAnimation = class("MJDiceAnimation", cc.UIView);
-
- function MJDiceAnimation:ctor()
- MJDiceAnimation.super.ctor(self)
- cc.SpriteFrameCache:getInstance():addSpriteFramesWithFile("mj/res/ui/zy_fangjian/animation/anim_dice/mj_dice.plist");
- cc.SpriteFrameCache:getInstance():addSpriteFramesWithFile("mj/res/ui/zy_fangjian/animation/anim_dice/mj_dice_anim.plist");
- cc.SpriteFrameCache:getInstance():addSpriteFramesWithFile("mj/res/ui/zy_fangjian/animation/anim_dice/mj_dice_anim2.plist");
- self.isPlaying = false; -- 是否正在播放动画
- self.animRoot = cc.Node:create();
- self:addChild(self.animRoot);
- end
-
- function MJDiceAnimation:onEnter()
- MJDiceAnimation.super.onEnter(self)
- end
-
- function MJDiceAnimation:onExit()
- MJDiceAnimation.super.onExit(self)
- end
- --[[
- -- 初始化第一帧
- -- @param
- -- @return
- --]]
- function MJDiceAnimation:initFirstFrame ()
- self.animImg = cc.ImageView:createNode()
- self.animImg:loadTexture(string.format("mj_dice_anim_%d.png", 0), cc.TextureResType.plistType)
- self.animRoot:addChild(self.animImg);
- end
- function MJDiceAnimation:initFirstFrame2 ()
- self.animImg = cc.ImageView:createNode()
- self.animImg:loadTexture(string.format("skeleton-animation_%d.png", 0), cc.TextureResType.plistType)
- self.animRoot:addChild(self.animImg);
- end
- --[[
- -- 播放动画
- -- @param dice1 骰子1的点数
- -- @param dice2 骰子2的点数
- -- @return
- --]]
- function MJDiceAnimation: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.015;
- 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_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 dice1 骰子1的点数
- -- @param dice2 骰子2的点数
- -- @return
- --]]
- function MJDiceAnimation:play2 (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.03;
- local currentFrame = 0;
- local totalFrame = 33;
- local maxFrame = totalFrame * 1;
- 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("skeleton-animation_%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.onAnimFinished2));
- local acSequence = cc.Sequence:create(acRepeat, acCallFunc);
- self.animImg:runAction(acSequence);
- self.animImg:setVisible(true);
- end
- --[[
- -- 停止播放动画
- -- @param
- -- @return
- --]]
- function MJDiceAnimation: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 MJDiceAnimation:removeSelf ()
- self:removeFromParent();
- self = nil;
- end
- --[[
- -- 动画播放结束事件
- -- @param
- -- @return
- --]]
- function MJDiceAnimation:onAnimFinished( )
- self:stop();
- self:showDiceResult(self.dice1, self.dice2);
- end
- function MJDiceAnimation:onAnimFinished2( )
- self:stop();
- self:showDiceResult2(self.dice1, self.dice2);
- end
- --[[
- -- 创建一颗骰子
- -- @param
- -- @return
- --]]
- function MJDiceAnimation:createDice (dice)
- local imgName = string.format("mj_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 MJDiceAnimation: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
- function MJDiceAnimation:showDiceResult2 (dice1, dice2)
- local imgDice1 = self:createDice(dice1);
- imgDice1:setPosition(cc.p(52, 56));
- self.animRoot:addChild(imgDice1);
-
- local imgDice2 = self:createDice(dice2);
- imgDice2:setPosition(cc.p(-52, -56));
- 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 MJDiceAnimation;
|