***游戲開發--轉盤抽獎邏輯思路***
> 引擎:CocosCreator > 語言:JS
思路:無論什么語言和引擎所有思路和邏輯是互通的,接下來要說的均為本地獲取資料;轉盤抽獎的方式很多種,我們只說其中最簡單實用易理解的方式--按照角度值計算
1>比如轉盤等分為8份獎品區,每個區域的角度自然為45度。
2>根據策劃的隨機概率隨機出數值,跟具數值判斷應該旋轉的度數。
其實就這兩條,抽獎程序中其實獎品已經獲得,轉盤旋轉只不過是效果而已,如果作為一個新手還不太情況接下來請看代碼展示
```
這里我就按照轉盤等分8份舉例,首先搞一個隨機概率
//100 200 300 400 500 600 700 900 -----獎品
//40% 25% 15% 10% 4% 3% 2% 1% -----概率
this.randomArray = [];
for(let i = 1; i<= 40; i++){
this.randomArray.push("100");
}
繼續回圈按照不同比例push到陣列中.........
```
```
然后最起碼要有一個按鈕吧,點擊抽獎;
calculateCallBack: function(){//按鈕回呼
let date = new Date();
let lastTime = cc.sys.localStorage.getItem("lastTime");
if(lastTime == null){
cc.sys.localStorage.setItem("lastTime",date.getTime());
this.randomReward();//這才是驅動轉盤旋轉,其他的可以不要;其他作用:點擊一次免費抽獎在點擊的話需要間隔24時
}
else{
let intervalTime = (date.getTime() - lastTime) / 1000;
if(intervalTime > 24 * 60 * 60){
cc.sys.localStorage.setItem("lastTime",date.getTime());
this.randomReward();
}
else{
//呼叫播放視頻
}
}
},
//具體功能可根據自己的需求添加,這都不是事
```
```
最重要的也就是轉盤轉動
randomReward: function(){
let rand = Math.floor(Math.random() * 100);
let random_Angle = null;//隨機度數
let random_TurnsNumber = null;//隨機圈數
this.reward = null;
switch(this.randomArray[rand]){
case "100":
//減去當前的角度:就是多次轉動后轉盤停止后的一個角度;this.rotaryTable代表轉盤
random_Angle = Math.floor(Math.random() * 45) + 0 - this.rotaryTable.angle % 360;
//獲取獎勵邏輯
break;
case "200":
random_Angle = Math.floor(Math.random() * 45) + 45 - this.rotaryTable.angle % 360;
break;
case "300":
random_Angle = Math.floor(Math.random() * 45) + 90 - this.rotaryTable.angle % 360;
break;
繼續case....................
}
random_TurnsNumber = Math.floor(Math.random() * 6 ) + 12;//隨機轉動的一個圈數
let sumAngle = 360 * random_TurnsNumber + random_Angle;//當前需要轉盤旋轉的角度
this.rotaryTable.runAction(cc.rotateTo(8,sumAngle).easing(cc.easeInOut(3.0)));
},
```
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/29131.html
標籤:Cocos2d-x
上一篇:Unity3d2018 實作場景淡入淡出 卻無回應 求指教
下一篇:哪里有虛幻4引擎的完整視頻教程?
