查找车轮的值命运之轮车轮、命运

2023-09-11 04:05:16 作者:理想三旬

我的财富轮车轮的形象,我试图让这样,当它旋转它显示正确的金额它所纺到。

I have an image of a wheel of fortune wheel and I am trying to make so that when it spins it displays the correct amount for what it was spun to.

我有以下的code: http://jsfiddle.net/maniator/rR67s/

I have the following code: http://jsfiddle.net/maniator/rR67s/

很多时候,它是正确的,其他的时间则是错误的。

Many times it is correct, and other time is is wrong.

例如我纺这样的:

和它提醒 300 ,这是不对的。

And it alerted 300, which is wrong.

如何解决我的算法,以便它是正确的99%的时间(如果有可能100%)?

How can I fix my algorithm so that it is correct 99% of the time (or 100% if it is possible)?

HTML:

<div id="game">
    <div id="tick">⇩</div>
    <img id="wheel" src="https://m.xsw88.com/allimgs/daicuo/20230911/2705.png" data-rotation="0">
</div>

JavaScript的:

Javascript:

var Wheel = (function () {
    var wheel = document.getElementById('wheel'),
        wheelValues = [5000, 600, 500, 300, 500, 800, 550, 400, 300, 900, 500, 300, 900, 0, 600, 400, 300, -2, 800, 350, 450, 700, 300, 600],
        spinTimeout = false,
        spinModifier = function () {
            return Math.random() * 10 + 20;
        },
        modifier = spinModifier(),
        slowdownSpeed = 0.5,
        prefix = (function () {
            if (document.body.style.MozTransform !== undefined) {
                return "MozTransform";
            } else if (document.body.style.WebkitTransform !== undefined) {
                return "WebkitTransform";
            } else if (document.body.style.OTransform !== undefined) {
                return "OTransform";
            } else {
                return "";
            }
        }()),
        degreeToRadian = function (deg) {
            return deg / (Math.PI * 180);
        };

    function Wheel() {};

    Wheel.prototype.rotate = function (degrees) {
        var val = "rotate(-" + degrees + "deg)";
        if (wheel.style[prefix] != undefined) wheel.style[prefix] = val;
        var rad = degreeToRadian(degrees % 360),
            filter = "progid:DXImageTransform.Microsoft.Matrix(sizingMethod='auto expand', M11=" + rad + ", M12=-" + rad + ", M21=" + rad + ", M22=" + rad + ")";
        if (wheel.style["filter"] != undefined) wheel.style["filter"] = filter;
        wheel.setAttribute("data-rotation", degrees);
    };

    Wheel.prototype.spin = function (callback, amount) {
        var _this = this;
        clearTimeout(spinTimeout);
        modifier -= slowdownSpeed;
        if (amount === undefined) {
            amount = parseInt(wheel.getAttribute('data-rotation'));
        }
        this.rotate(amount);
        if (modifier > 0) {
            spinTimeout = setTimeout(function () {
                _this.spin(callback, amount + modifier);
            }, 1000 / 5);
        } else {
            var dataRotation = parseInt(wheel.getAttribute('data-rotation'));
            modifier = spinModifier();
            var divider = 360 / wheelValues.length;
            var wheelValue = wheelValues[Math.floor(Math.round(dataRotation % 360) / divider)];
            switch (wheelValue) {
                case 0:
                    return callback(0);
                case -1:
                    return callback("Free Spin");
                case -2:
                    return callback("Lose a turn");
                default:
                    return callback(wheelValue);
            }
        }
    };

    return Wheel;
})();    

var wheel = new Wheel;
wheel.spin(function(spinVal){
    alert(spinVal)
});

全场比赛为那些谁想要尝试一下: http://jsfiddle.net/maniator/XP9Qv/ (小于 - 这是用公认的答案更新)

Full game for those who want to try it out: http://jsfiddle.net/maniator/XP9Qv/ (<-- this was updated using accepted answer)

这项活动继续这里。

推荐答案

我认为这个问题是,在起始位置的箭头是在区域的中间,而不是在它的开始。所以你有一个起始偏移(360 / wheelValues​​.length)/ 2

I think the problem is that the arrow in the starting position is in the middle of a zone, not at the start of it. So you have a starting offset of (360 / wheelValues.length)/2

var divider = 360 / wheelValues.length;
var offset=divider/2; //half division
var wheelValue = wheelValues[Math.floor(Math.ceil((dataRotation+offset) % 360) / divider)];

这似乎工作:当车轮停止或者在开始时(前半),或在一个区段的结尾(后半)的显示​​值是所预期的(只是大约十几个测试完成)

This seems to work: when the wheel stops either at the beginning (first half) or at the end (last half) of a zone the showed value is the expected one (just about a dozen tests done)