1. 程式人生 > >cocos lua轉盤抽獎的核心程式碼

cocos lua轉盤抽獎的核心程式碼

    local totalCount = 12  -- 轉盤總獎項數
    local roundCountMin = 2  -- 轉動最小圈數
    local roundCountMax = 11  -- 轉動最大圈數

    local singleAngle = 360 / totalCount  -- 所有獎項概率相同時 這樣計算每個獎項佔的角度 如果概率不同,可以使用table陣列來處理
    local offsetAngle = 7  -- 為了避免不必要的麻煩,在接近2個獎項的交界處,左右偏移n角度的位置,統統不停留 否則停在交界線上,很難解釋清楚 這個值必須小於最小獎項所佔角度的1/2
-- 設定隨機數種子 正常情況下這應該在初始化時做 而不是在呼叫函式時 math.randomseed(os.time()) -- 預設隨機獎項 if stopId == nil or stopId < totalCount then stopId = math.random(math.random(totalCount-7),totalCount) -- 產生totalCount之間的整數 end -- 轉盤停止位置的最小角度 不同概率時,直接把之前的項相加即可 local angleMin = (stopId-1
) * singleAngle -- 轉盤轉動圈數 目前隨機 正常情況下可加入力量元素 根據 移動距離*引數 計算轉動圈數 local roundCount = math.random(roundCountMin, roundCountMax) -- 產生roundCountMin-roundCountMax之間的整數 -- 檢查一下跳過角度是否合法 當前獎項角度-2*跳過角度 結果必須>0 TODO -- 轉動角度 local angleTotal = 360*roundCount + angleMin + math.random(offsetAngle, singleAngle-offsetAngle) -- 避免掉offsetAngle角度的停留,防止停留在交界線上
-- 列印資料 print('stopId:'..stopId) print('angleMin:'..angleMin) print('roundCount:'..roundCount) print('angleTotal:'..angleTotal) self.main_layer = self.widget:getChildByName("main") self.bg=self.main_layer:getChildByName("bg"):getChildByName("wheels") --查詢轉盤 self.award=self.main_layer:getChildByName("Img_award") -- 復位轉盤 bg:setRotation(0) award:setRotation(0) -- 開始旋轉動作 使用EaseExponentialOut(迅速加速,然後慢慢減速) bg:runAction(cc.EaseExponentialOut:create(cc.RotateBy:create(3.0, angleTotal))) award:runAction(cc.EaseExponentialOut:create(cc.RotateBy:create(3.0, angleTotal)))