quick-cocos2dx-lua之幸运大转盘实现

好久没有用quick引擎开发了,最近又要整quick了,有种熟悉感。

今天主要记录一下研究了半天的大转盘的实现。

如图:

quick-cocos2dx-lua之幸运大转盘实现_第1张图片

1、主要精华如下:

local ALLROATE = 360--360度
local num = 10
--转盘角度数据
local zhuanpanData =
{
    {start = (num-10)*ALLROATE/num + 0, ended = (num-9)*ALLROATE/num},
    {start = (num-9)*ALLROATE/num + 1, ended = (num-8)*ALLROATE/num},
    {start = (num-8)*ALLROATE/num + 1, ended = (num-7)*ALLROATE/num},
    {start = (num-7)*ALLROATE/num + 1, ended = (num-6)*ALLROATE/num},
    {start = (num-6)*ALLROATE/num + 1, ended = (num-5)*ALLROATE/num},
    {start = (num-5)*ALLROATE/num + 1, ended = (num-4)*ALLROATE/num},
    {start = (num-4)*ALLROATE/num + 1, ended = (num-3)*ALLROATE/num},
    {start = (num-3)*ALLROATE/num + 1, ended = (num-2)*ALLROATE/num},
    {start = (num-2)*ALLROATE/num + 1, ended = (num-1)*ALLROATE/num},
    {start = (num-1)*ALLROATE/num + 1, ended = (num-0)*ALLROATE/num},
}

local duration  = 5 --转动持续时间
local rotateNum = 5 --转动圈数
local function Act()
local targetIdx = 7   --服务器传来的值。
scheduler.performWithDelayGlobal(function(dt)
            local targetData = zhuanpanData[targetIdx]
            local rotateAngle = - math.random(targetData.start, targetData.ended) - 360 * rotateNum
            print("随机角度是:", rotateAngle)
            --第二次需要重置坐标点
            if lastAngle ~= 0 then
            self:rotateSprite(self.panbg, duration, rotateAngle + lastAngle)
      else
          self:rotateSprite(self.panbg, duration, rotateAngle)
    end
            lastAngle = -360 - rotateAngle - 360 * rotateNum
        end, 0)
end

2、封装转盘的主要method,直接复制调用然后传参数就可以啦,请仔细看参数说明

-- sprite 精灵
-- time 旋转的时间
-- rotateAngle_ 旋转的角度
local function rotateSprite(sprite, time, rotateAngle_)
local action = cc.RotateBy:create(time, rotateAngle_)
    local easeAction = cc.EaseCubicActionInOut:create(action)
    sprite:runAction(easeAction)
end

如果有更好的实现效果请,留言一同探讨。

你可能感兴趣的:(quick-cocos2dx)