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.

142 lines
4.1 KiB

  1. local NeiJiangMJDiceAnimation = class("NeiJiangMJDiceAnimation", cc.UIView);
  2. function NeiJiangMJDiceAnimation:ctor()
  3. NeiJiangMJDiceAnimation.super.ctor(self)
  4. cc.SpriteFrameCache:getInstance():addSpriteFramesWithFile("mj_neijiang/res/zy_fangjian/mj_neijiang_dice.plist");
  5. cc.SpriteFrameCache:getInstance():addSpriteFramesWithFile("mj_neijiang/res/zy_fangjian/mj_neijiang_dice_anim.plist");
  6. self.isPlaying = false; -- 是否正在播放动画
  7. self.animRoot = cc.Node:create();
  8. self:addChild(self.animRoot);
  9. end
  10. function NeiJiangMJDiceAnimation:onEnter()
  11. NeiJiangMJDiceAnimation.super.onEnter(self)
  12. end
  13. function NeiJiangMJDiceAnimation:onExit()
  14. NeiJiangMJDiceAnimation.super.onExit(self)
  15. end
  16. --[[
  17. -- 初始化第一帧
  18. -- @param
  19. -- @return
  20. --]]
  21. function NeiJiangMJDiceAnimation:initFirstFrame ()
  22. self.animImg = cc.ImageView:createNode()
  23. self.animImg:loadTexture(string.format("mj_neijiang_dice_anim_%d.png", 0), cc.TextureResType.plistType)
  24. self.animRoot:addChild(self.animImg);
  25. end
  26. --[[
  27. -- 播放动画
  28. -- @param dice1 骰子1的点数
  29. -- @param dice2 骰子2的点数
  30. -- @return
  31. --]]
  32. function NeiJiangMJDiceAnimation:play (dice1, dice2, callback)
  33. if self.isPlaying then
  34. return ;
  35. end
  36. self.callback = callback;
  37. self.dice1 = dice1;
  38. self.dice2 = dice2;
  39. self.isPlaying = true;
  40. self:initFirstFrame();
  41. -- 循环修改图片
  42. local frameTime = 0.025;
  43. local currentFrame = 0;
  44. local totalFrame = 13;
  45. local maxFrame = totalFrame * 2;
  46. local action1 = cc.Sequence:create(cc.DelayTime:create(frameTime), cc.CallFunc:create(function ()
  47. if currentFrame > maxFrame then
  48. self:stop();
  49. return ;
  50. end
  51. local imgIndex = (currentFrame + 1) % totalFrame;
  52. imgIndex = imgIndex == 0 and totalFrame or imgIndex;
  53. local imgName = string.format("mj_neijiang_dice_anim_%d.png", (imgIndex - 1))
  54. if not tolua.isnull(self.animImg) then
  55. self.animImg:loadTexture(imgName, cc.TextureResType.plistType)
  56. end
  57. currentFrame = currentFrame + 1
  58. end))
  59. local acRepeat = cc.Repeat:create(action1, maxFrame);
  60. local acCallFunc = cc.CallFunc:create(handler(self, self.onAnimFinished));
  61. local acSequence = cc.Sequence:create(acRepeat, acCallFunc);
  62. self.animImg:runAction(acSequence);
  63. self.animImg:setVisible(true);
  64. end
  65. --[[
  66. -- 停止播放动画
  67. -- @param
  68. -- @return
  69. --]]
  70. function NeiJiangMJDiceAnimation:stop ()
  71. if not self.isPlaying then
  72. return ;
  73. end
  74. self.isPlaying = false;
  75. if self.animImg then
  76. self.animImg:stopAllActions();
  77. self.animImg:setVisible(false);
  78. end
  79. end
  80. --[[
  81. -- 移除本身
  82. -- @param
  83. -- @return
  84. --]]
  85. function NeiJiangMJDiceAnimation:removeSelf ()
  86. self:removeFromParent();
  87. self = nil;
  88. end
  89. --[[
  90. -- 动画播放结束事件
  91. -- @param
  92. -- @return
  93. --]]
  94. function NeiJiangMJDiceAnimation:onAnimFinished( )
  95. self:stop();
  96. self:showDiceResult(self.dice1, self.dice2);
  97. end
  98. --[[
  99. -- 创建一颗骰子
  100. -- @param
  101. -- @return
  102. --]]
  103. function NeiJiangMJDiceAnimation:createDice (dice)
  104. local imgName = string.format("mj_neijiang_dice_l_%d.png", dice);
  105. local imgDice = cc.ImageView:createNode();
  106. imgDice:loadTexture(imgName, cc.TextureResType.plistType);
  107. imgDice:setScale(1.1);
  108. return imgDice;
  109. end
  110. --[[
  111. -- 显示两个摇骰结果
  112. -- @param
  113. -- @return
  114. --]]
  115. function NeiJiangMJDiceAnimation:showDiceResult (dice1, dice2)
  116. local imgDice1 = self:createDice(dice1);
  117. imgDice1:setPosition(cc.p(40, 70));
  118. self.animRoot:addChild(imgDice1);
  119. local imgDice2 = self:createDice(dice2);
  120. imgDice2:setPosition(cc.p(-40, -70));
  121. self.animRoot:addChild(imgDice2);
  122. local acDelay = cc.DelayTime:create(1.5);
  123. local acCallFunc = cc.CallFunc:create(function ()
  124. self:stopAllActions();
  125. self:removeSelf();
  126. if self.callback and type(self.callback) == "function" then
  127. self.callback(dice1, dice2);
  128. end
  129. end);
  130. local acSequence = cc.Sequence:create(acDelay, acCallFunc);
  131. self:runAction(acSequence);
  132. end
  133. return NeiJiangMJDiceAnimation;