CocosCreator用概率实现轮盘抽奖确定不来看看吗

半年了终于更博客了。

轮盘抽奖是大多数小游戏必备的一个功能,之前在项目中有用到,故今天也算是自己再复习下并分享给大家,如有不足望提出

效果图:

CocosCreator用概率实现轮盘抽奖确定不来看看吗_第1张图片

图片从网上找的比较粗糙...咳咳主要实现功能为主

直接贴出代码好了,已经写好可以ctrl+c+v放到自己项目中使用,如有不懂也可以在文章结尾下载demo来查看

cocos版本2.3.0,使用语言ts

import GAME_DATE from "./GAME_DATE";

class dialDate  {
    private static key_award_id = ["key_award_id"];
    private static data = [];
    private static dialArawd;

    private static m_rotate;
    private static m_awardId;
    /**  lotterDate:抽奖回调(奖励数据) */
    public static initData(res, lotterDate) {
        if (this.data.length == 0) {
            cc.loader.loadRes(res, function (err,data:cc.JsonAsset) {
                if (err) {
                    cc.error(err.message || err);
                    return;
                }
                this.data = data.json;
            }.bind(this));
        }

        this.dialArawd = lotterDate;
    }

    public static clearData() {
        this.data = [];
    }

    public static getRotate() {
        var once_award = GAME_DATE.getJsonDate(this.key_award_id);
        if (once_award == null) {
            once_award = {};
        }
        var probability = 0;
        for (var i = 0; i < this.data.length; i++) {
            if (!once_award[i]) {
                probability += Math.floor(this.data[i].probability);
            }
        }
        var value = GAME_DATE.getRand(1, probability);
        var index = 0;
        for (var i = 0; i < this.data.length; i++) {
            if (!once_award[i]) {
                if (value > this.data[i].probability) {
                    value -= this.data[i].probability;
                } else {
                    index = i;
                    break;
                }
            }
        }

        this.m_awardId = this.data[index].id;
        if (this.data[index].rotate.length == 1) {
            this.m_rotate = this.data[index].rotate[0];
        } else {
            this.m_rotate = GAME_DATE.getRand(this.data[index].rotate[0], this.data[index].rotate[1]);
        }
        return this.m_rotate;
    }

    public static getAward() {
        for (var i = 0; i < this.data.length; i++) {
            if (this.m_awardId == this.data[i].id) {
                this.dialArawd(this.data[i]);
                return this.data[i];
            }
        }
    }

}


const {ccclass, property} = cc._decorator;

@ccclass
export default class dial extends cc.Component {

    @property(cc.Node)//转盘
    dial: cc.Node = null;
    @property(cc.Label)//提示
    hintLabel: cc.Label = null;
    
    @property(cc.Integer)//总共有几个选项
    dialNum: number = 10;
    @property(cc.Float)//转动时间
    dialTime: number = 7;

    isBegin = false;
    clickTime = 0;

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        //传入json路径读取json配置
        dialDate.initData("json/dialDate", (data) => {
            this.hintLabel.string = data.awardInfo

            switch (data.awardType) {
                case 0://红包
                    GAME_DATE.setDiamond(data.awardNum)
                    break;
                case 1://原谅卡等
                    break;
                case 2:
                    break;
            }
        });
    }

    //开始转盘移动
    runStart() {
        var value = dialDate.getRotate();
        var rotate = value - (this.dial.angle % 360) + 360 * this.dialNum;
        this.dial.runAction(cc.sequence(cc.rotateBy(this.dialTime, rotate).easing(cc.easeInOut(2)),
            cc.callFunc(this.getAward.bind(this))));
    }


     //这里可以写转盘结束后的事件
    getAward() {
        dialDate.getAward();
        this.isBegin = false;
    }

    start () {

    }

    onClickBtn(){
        if (this.isBegin) { //如果正在抽奖给出提示
            cc.log("正在抽奖请稍后");
            return;
        }
        this.isBegin = true;
        this.runStart();
    }

    update() {
        if (this.clickTime > 0) {
            this.clickTime -= 0.016;
            if (this.clickTime < 0) {
                this.isBegin = false;
            }
        }

    }
}

export default class GAME_DATE {

    static getJsonDate(key, append?){
        if (typeof key != "string") {
            if (key[1] === undefined) {
                key[1] = this.getJsonDate(this.getKey(key[0], append));
            }
            return key[1];
        }
        var _value = cc.sys.localStorage.getItem(this.getKey(key, append));
        if (_value === null || _value === "") {
            return null;
        }
        try {
            return JSON.parse(_value);
        }
        catch (err) {
            //数据不是json格式
            return _value;
        }
    }

    static getRand(start: number, end: number){
        if (start >= end) {
            return start;
        }
        return Math.floor(Math.random() * (end - start + 1) + start);
    }

    static getKey(key, append) {
        if (append) {
            return append + "_" + key;
        }
        return key;
    }

    static setDiamond(diamond){
        cc.sys.localStorage.setItem("diamond",diamond);
    }

    static getDsiamond(){
        let diamond = cc.sys.localStorage.getItem("diamond");
        return diamond;
    }

}

百度网盘地址:链接:https://pan.baidu.com/s/1CYW87OTj_2xZcFcjp4KYXQ  提取码:36mj

你可能感兴趣的:(Cocos,Creator学习)