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.

214 lines
6.4 KiB

  1. local MJDiceAnimation = class("MJDiceAnimation", cc.UIView);
  2. function MJDiceAnimation:ctor()
  3. MJDiceAnimation.super.ctor(self)
  4. cc.SpriteFrameCache:getInstance():addSpriteFramesWithFile("mj/res/ui/zy_fangjian/animation/anim_dice/mj_dice.plist");
  5. cc.SpriteFrameCache:getInstance():addSpriteFramesWithFile("mj/res/ui/zy_fangjian/animation/anim_dice/mj_dice_anim.plist");
  6. cc.SpriteFrameCache:getInstance():addSpriteFramesWithFile("mj/res/ui/zy_fangjian/animation/anim_dice/mj_dice_anim2.plist");
  7. self.isPlaying = false; -- 是否正在播放动画
  8. self.animRoot = cc.Node:create();
  9. self:addChild(self.animRoot);
  10. end
  11. function MJDiceAnimation:onEnter()
  12. MJDiceAnimation.super.onEnter(self)
  13. end
  14. function MJDiceAnimation:onExit()
  15. MJDiceAnimation.super.onExit(self)
  16. end
  17. --[[
  18. -- 初始化第一帧
  19. -- @param
  20. -- @return
  21. --]]
  22. function MJDiceAnimation:initFirstFrame ()
  23. self.animImg = cc.ImageView:createNode()
  24. self.animImg:loadTexture(string.format("mj_dice_anim_%d.png", 0), cc.TextureResType.plistType)
  25. self.animRoot:addChild(self.animImg);
  26. end
  27. function MJDiceAnimation:initFirstFrame2 ()
  28. self.animImg = cc.ImageView:createNode()
  29. self.animImg:loadTexture(string.format("skeleton-animation_%d.png", 0), cc.TextureResType.plistType)
  30. self.animRoot:addChild(self.animImg);
  31. end
  32. --[[
  33. -- 播放动画
  34. -- @param dice1 骰子1的点数
  35. -- @param dice2 骰子2的点数
  36. -- @return
  37. --]]
  38. function MJDiceAnimation:play (dice1, dice2, callback)
  39. if self.isPlaying then
  40. return ;
  41. end
  42. self.callback = callback;
  43. self.dice1 = dice1;
  44. self.dice2 = dice2;
  45. self.isPlaying = true;
  46. self:initFirstFrame();
  47. -- 循环修改图片
  48. local frameTime = 0.015;
  49. local currentFrame = 0;
  50. local totalFrame = 13;
  51. local maxFrame = totalFrame * 2;
  52. local action1 = cc.Sequence:create(cc.DelayTime:create(frameTime), cc.CallFunc:create(function ()
  53. if currentFrame > maxFrame then
  54. self:stop();
  55. return ;
  56. end
  57. local imgIndex = (currentFrame + 1) % totalFrame;
  58. imgIndex = imgIndex == 0 and totalFrame or imgIndex;
  59. local imgName = string.format("mj_dice_anim_%d.png", (imgIndex - 1))
  60. if not tolua.isnull(self.animImg) then
  61. self.animImg:loadTexture(imgName, cc.TextureResType.plistType)
  62. end
  63. currentFrame = currentFrame + 1
  64. end))
  65. local acRepeat = cc.Repeat:create(action1, maxFrame);
  66. local acCallFunc = cc.CallFunc:create(handler(self, self.onAnimFinished));
  67. local acSequence = cc.Sequence:create(acRepeat, acCallFunc);
  68. self.animImg:runAction(acSequence);
  69. self.animImg:setVisible(true);
  70. end
  71. --[[
  72. -- 播放动画 第二种骰子动画
  73. -- @param dice1 骰子1的点数
  74. -- @param dice2 骰子2的点数
  75. -- @return
  76. --]]
  77. function MJDiceAnimation:play2 (dice1, dice2, callback)
  78. if self.isPlaying then
  79. return ;
  80. end
  81. self.callback = callback;
  82. self.dice1 = dice1;
  83. self.dice2 = dice2;
  84. self.isPlaying = true;
  85. self:initFirstFrame();
  86. -- 循环修改图片
  87. local frameTime = 0.03;
  88. local currentFrame = 0;
  89. local totalFrame = 33;
  90. local maxFrame = totalFrame * 1;
  91. local action1 = cc.Sequence:create(cc.DelayTime:create(frameTime), cc.CallFunc:create(function ()
  92. if currentFrame > maxFrame then
  93. self:stop();
  94. return ;
  95. end
  96. local imgIndex = (currentFrame + 1) % totalFrame;
  97. imgIndex = imgIndex == 0 and totalFrame or imgIndex;
  98. local imgName = string.format("skeleton-animation_%d.png", (imgIndex - 1))
  99. if not tolua.isnull(self.animImg) then
  100. self.animImg:loadTexture(imgName, cc.TextureResType.plistType)
  101. end
  102. currentFrame = currentFrame + 1
  103. end))
  104. local acRepeat = cc.Repeat:create(action1, maxFrame);
  105. local acCallFunc = cc.CallFunc:create(handler(self, self.onAnimFinished2));
  106. local acSequence = cc.Sequence:create(acRepeat, acCallFunc);
  107. self.animImg:runAction(acSequence);
  108. self.animImg:setVisible(true);
  109. end
  110. --[[
  111. -- 停止播放动画
  112. -- @param
  113. -- @return
  114. --]]
  115. function MJDiceAnimation:stop ()
  116. if not self.isPlaying then
  117. return ;
  118. end
  119. self.isPlaying = false;
  120. if self.animImg then
  121. self.animImg:stopAllActions();
  122. self.animImg:setVisible(false);
  123. end
  124. end
  125. --[[
  126. -- 移除本身
  127. -- @param
  128. -- @return
  129. --]]
  130. function MJDiceAnimation:removeSelf ()
  131. self:removeFromParent();
  132. self = nil;
  133. end
  134. --[[
  135. -- 动画播放结束事件
  136. -- @param
  137. -- @return
  138. --]]
  139. function MJDiceAnimation:onAnimFinished( )
  140. self:stop();
  141. self:showDiceResult(self.dice1, self.dice2);
  142. end
  143. function MJDiceAnimation:onAnimFinished2( )
  144. self:stop();
  145. self:showDiceResult2(self.dice1, self.dice2);
  146. end
  147. --[[
  148. -- 创建一颗骰子
  149. -- @param
  150. -- @return
  151. --]]
  152. function MJDiceAnimation:createDice (dice)
  153. local imgName = string.format("mj_dice_l_%d.png", dice);
  154. local imgDice = cc.ImageView:createNode();
  155. imgDice:loadTexture(imgName, cc.TextureResType.plistType);
  156. imgDice:setScale(1.1);
  157. return imgDice;
  158. end
  159. --[[
  160. -- 显示两个摇骰结果
  161. -- @param
  162. -- @return
  163. --]]
  164. function MJDiceAnimation:showDiceResult (dice1, dice2)
  165. local imgDice1 = self:createDice(dice1);
  166. imgDice1:setPosition(cc.p(40, 70));
  167. self.animRoot:addChild(imgDice1);
  168. local imgDice2 = self:createDice(dice2);
  169. imgDice2:setPosition(cc.p(-40, -70));
  170. self.animRoot:addChild(imgDice2);
  171. local acDelay = cc.DelayTime:create(1.5);
  172. local acCallFunc = cc.CallFunc:create(function ()
  173. self:stopAllActions();
  174. self:removeSelf();
  175. if self.callback and type(self.callback) == "function" then
  176. self.callback(dice1, dice2);
  177. end
  178. end);
  179. local acSequence = cc.Sequence:create(acDelay, acCallFunc);
  180. self:runAction(acSequence);
  181. end
  182. function MJDiceAnimation:showDiceResult2 (dice1, dice2)
  183. local imgDice1 = self:createDice(dice1);
  184. imgDice1:setPosition(cc.p(52, 56));
  185. self.animRoot:addChild(imgDice1);
  186. local imgDice2 = self:createDice(dice2);
  187. imgDice2:setPosition(cc.p(-52, -56));
  188. self.animRoot:addChild(imgDice2);
  189. local acDelay = cc.DelayTime:create(1.5);
  190. local acCallFunc = cc.CallFunc:create(function ()
  191. self:stopAllActions();
  192. self:removeSelf();
  193. if self.callback and type(self.callback) == "function" then
  194. self.callback(dice1, dice2);
  195. end
  196. end);
  197. local acSequence = cc.Sequence:create(acDelay, acCallFunc);
  198. self:runAction(acSequence);
  199. end
  200. return MJDiceAnimation;