Cocos Creator实例-制作抽奖池

出现空白,请点击下方[阅读更多]
转载地址:https://blog.csdn.net/u011607490/article/details/82701325

先看看层级结构

 

给guide节点添加3个动画clip

首先是rotationStart旋转开始

添加rotation属性

设置两个关键帧,rotation=45、rotation=405,

拖动红线到一个位置,改变节点rotation属性,即可设置关键帧。

下面,sample为帧数、speed为播放速度、WrapMode为播放模式,normal为正常、loop为循环等

然后设置缓动作

选择Ease In Sine

然后类似的做剩下的动画,

rotating、rotationFinish (其中rotating不需要缓动作但需要WrapMode为loop循环,rotationFinish需要Ease Out Sine)

random是这个抽奖池的根节点,给它挂上脚本组件

然后编写脚本RandPlat.js

 先设置一些属性接口


  
  
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. //奖品数组
  5. gifts: {
  6. //默认类型为数组
  7. default: [],
  8. //元素类型为cc.Sprite类型
  9. type: [cc.Sprite]
  10. },
  11. //guide旋转盘节点
  12. plat:cc.Node,
  13. //转盘上的点击抽奖字样Label
  14. inforLabel:cc.Label,
  15. //旁边显示当前序号的Layout
  16. resultLayout:cc.Node,
  17. },
  18. })

拖入相应节点 

然后在onload里初始化一些需要的值


  
  
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. gifts: {
  5. default: [],
  6. type: [cc.Sprite]
  7. },
  8. plat:cc.Node,
  9. inforLabel:cc.Label,
  10. resultLayout:cc.Node,
  11. },
  12. //添加几个需要间接利用的属性
  13. originRota: null,
  14. animPlay: null,
  15. giftOrder: null,
  16. resultLabel: null,
  17. lastAngle: null,
  18. onLoad () {
  19. //记录转盘初始角度
  20. this.originRota = this.plat.rotation
  21. //记录转盘节点的动画组件
  22. this.animPlay = this.plat.getComponent(cc.Animation)
  23. //记录序号表节点上的序号Label
  24. this.resultLabel = this.resultLayout.getChildByName( 'result').getComponent(cc.Label)
  25. //初始化最后一次旋转角(下面会用到)
  26. this.lastAngle = this.originRota
  27. },
  28. })

然后给转盘plat添加点击事件


  
  
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. gifts: {
  5. default: [],
  6. type: [cc.Sprite]
  7. },
  8. plat:cc.Node,
  9. inforLabel:cc.Label,
  10. resultLayout:cc.Node,
  11. },
  12. originRota: null,
  13. animPlay: null,
  14. giftOrder: null,
  15. resultLabel: null,
  16. lastAngle: null,
  17. onLoad () {
  18. this.originRota = this.plat.rotation
  19. this.animPlay = this.plat.getComponent(cc.Animation)
  20. this.resultLabel = this.resultLayout.getChildByName( 'result').getComponent(cc.Label)
  21. this.lastAngle = this.originRota
  22. },
  23. start () {
  24. //当点击时
  25. this.plat.on( "touchstart", function(){
  26. cc.log( 'click')
  27. //开始播放旋转动画
  28. this.startPlay()
  29. }, this)
  30. },
  31. })

 然后我们实现startPlay方法


  
  
  1. //开始抽奖
  2. startPlay(){
  3. cc.log( 'startPlay')
  4. //取得播放信息
  5. var stateStart = this.animPlay.play( 'rotationStart')
  6. //从播放信息得到旋转角度数组的[0]元素(开始角度),并设置值为lastAngle
  7. this.getAngleClip(stateStart)[ 0] = this.lastAngle
  8. //转盘plat上面的label提示“等待结果”
  9. this.inforLabel.string = '等待\n结果'
  10. //设置定时器,“rotationStart”播放完播放“rotating”
  11. //stateStart.duration为“rotationStart”播放时长
  12. this.scheduleOnce( function(){
  13. this.processPlay()
  14. }, stateStart.duration)
  15. },

 然后实现getAngleClip方法和processPlay方法


  
  
  1. //得到rotationClip的旋转值数组
  2. getAngleClip(state){
  3. return state.curves[ 0].values
  4. },

  
  
  1. //正在抽奖
  2. processPlay(){
  3. cc.log( 'stateProcess')
  4. var stateProcess = this.animPlay.play( 'rotating')
  5. this.scheduleOnce( function(){
  6. //计时结束播放“rotationFinish”动画
  7. this.finishPlay()
  8. }, stateProcess.duration)
  9. },

 现在点击转盘可以旋转但是奖品还没有效果

在渲染函数update里添加渲染函数回调


  
  
  1. update (dt) {
  2. //当前角度映射到序号
  3. this.giftOrder = this.inforRotation( this.plat.rotation- this.originRota)
  4. //高亮该序号奖品
  5. this.highLightGift( this.giftOrder)
  6. //刷新序号Layout上的显示
  7. this.getResult()
  8. },

 现在实现旋转角度和奖品的映射关系


  
  
  1. //根据角度得到序号
  2. inforRotation(rotation){
  3. //角度 = 角度 - 360 * 圈数
  4. var _rotation = rotation - 360 * this.getRounds(rotation)
  5. // “360 / this.gifts.length” 为步长
  6. return parseInt(_rotation / ( 360 / this.gifts.length) )
  7. },

  
  
  1. //得到圈数
  2. getRounds(rotation){
  3. return parseInt(rotation / 360)
  4. },

  
  
  1. //高亮奖品
  2. highLightGift(order){
  3. //刷新所有奖品颜色为初始颜色
  4. this.refreshColor()
  5. //设置该序号奖品节点高亮
  6. this.gifts[order].node.color = cc.Color.GREEN
  7. },

  
  
  1. //刷新
  2. refreshColor(){
  3. //遍历奖品数组
  4. for( var _order = 0; _order < this.gifts.length; _order++){
  5. //设为白色
  6. this.gifts[_order].node.color = cc.Color.WHITE
  7. }
  8. },

  
  
  1. //显示结果
  2. getResult(){
  3. //设置序号表label显示
  4. this.resultLabel.string = this.giftOrder + 1
  5. },

最后实现随机停止得出结果finishPlay方法


  
  
  1. //停止抽奖
  2. finishPlay(){
  3. cc.log( 'finishPlay')
  4. var stateFinish = this.animPlay.play( 'rotationFinish')
  5. //再次旋转时,从上次的位置开始
  6. this.getAngleClip(stateFinish)[ 1] = this.originRota + this.randAngle()
  7. this.scheduleOnce( function(){
  8. //plat转盘字样变为“再次抽奖”
  9. this.inforLabel.string = '再次\n抽奖'
  10. //记录停止的角度
  11. this.lastAngle = this.getAngleClip(stateFinish)[ 1]
  12. }, stateFinish.duration)
  13. },

 实现随机角度


  
  
  1. //随机角度
  2. randAngle(){
  3. return parseInt( 360 * cc.random0To1())
  4. },

 最后代码应该是这样


  
  
  1. /**************************************************
  2. # Author : AK-12
  3. # Last modified: 2018-09-14 15:51
  4. # Email : [email protected]
  5. # github : https://github.com/Saber2pr
  6. # Filename : RandPlat.js
  7. # Description :
  8. **************************************************/
  9. cc.Class({
  10. extends: cc.Component,
  11. properties: {
  12. gifts: {
  13. default: [],
  14. type: [cc.Sprite]
  15. },
  16. plat:cc.Node,
  17. inforLabel:cc.Label,
  18. resultLayout:cc.Node,
  19. },
  20. originRota: null,
  21. animPlay: null,
  22. giftOrder: null,
  23. resultLabel: null,
  24. lastAngle: null,
  25. onLoad () {
  26. this.originRota = this.plat.rotation
  27. this.animPlay = this.plat.getComponent(cc.Animation)
  28. this.resultLabel = this.resultLayout.getChildByName( 'result').getComponent(cc.Label)
  29. this.lastAngle = this.originRota
  30. },
  31. start () {
  32. this.plat.on( "touchstart", function(){
  33. cc.log( 'click')
  34. this.startPlay()
  35. }, this)
  36. },
  37. update (dt) {
  38. this.giftOrder = this.inforRotation( this.plat.rotation- this.originRota)
  39. this.highLightGift( this.giftOrder)
  40. this.getResult()
  41. },
  42. //随机角度
  43. randAngle(){
  44. return parseInt( 360 * cc.random0To1())
  45. },
  46. //开始抽奖
  47. startPlay(){
  48. cc.log( 'startPlay')
  49. var stateStart = this.animPlay.play( 'rotationStart')
  50. this.getAngleClip(stateStart)[ 0] = this.lastAngle
  51. this.inforLabel.string = '等待\n结果'
  52. this.scheduleOnce( function(){
  53. this.processPlay()
  54. }, stateStart.duration)
  55. },
  56. //正在抽奖
  57. processPlay(){
  58. cc.log( 'stateProcess')
  59. var stateProcess = this.animPlay.play( 'rotating')
  60. this.scheduleOnce( function(){
  61. this.finishPlay()
  62. }, stateProcess.duration)
  63. },
  64. //得到rotationClip的旋转值数组
  65. getAngleClip(state){
  66. return state.curves[ 0].values
  67. },
  68. //停止抽奖
  69. finishPlay(){
  70. cc.log( 'finishPlay')
  71. var stateFinish = this.animPlay.play( 'rotationFinish')
  72. this.getAngleClip(stateFinish)[ 1] = this.originRota + this.randAngle()
  73. this.scheduleOnce( function(){
  74. this.inforLabel.string = '再次\n抽奖'
  75. this.lastAngle = this.getAngleClip(stateFinish)[ 1]
  76. }, stateFinish.duration)
  77. cc.log(stateFinish)
  78. },
  79. //得到圈数
  80. getRounds(rotation){
  81. return parseInt(rotation / 360)
  82. },
  83. //得到序号
  84. inforRotation(rotation){
  85. var _rotation = rotation - 360 * this.getRounds(rotation)
  86. return parseInt(_rotation / ( 360 / this.gifts.length) )
  87. },
  88. //刷新
  89. refreshColor(){
  90. for( var _order = 0; _order < this.gifts.length; _order++){
  91. this.gifts[_order].node.color = cc.Color.WHITE
  92. }
  93. },
  94. //高亮奖品
  95. highLightGift(order){
  96. this.refreshColor()
  97. this.gifts[order].node.color = cc.Color.GREEN
  98. },
  99. //停止结果
  100. getResult(){
  101. this.resultLabel.string = this.giftOrder + 1
  102. },
  103. });

github地址

https://github.com/Saber2pr/CocosCreatorExam

猜你喜欢

转载自blog.csdn.net/qq_40303068/article/details/89029113