javascript - 找到命运之轮的值(value)

标签 javascript algorithm

我有一张命运之轮的图片,我正在努力让它在旋转时显示正确的旋转量。

我有以下代码:http://jsfiddle.net/maniator/rR67s/

很多时候是对的,其他时候是错的。

例如我纺这个:

300

它警告了 300,这是错误的。

我怎样才能修正我的算法,使其在 99% 的时间内(或 100%,如果可能的话)都是正确的?

HTML:

<div id="game">
    <div id="tick">⇩</div>
    <img id="wheel" src="http://i.imgur.com/R7JYazp.png" data-rotation="0">
</div>

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/ (<-- 这是使用已接受的答案更新的)


乐趣继续here .

最佳答案

我认为问题在于起始位置的箭头位于区域的中间,而不是区域的开始。因此,您的起始偏移量为 (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)];

这似乎可行:当轮子在区域的开始(前半部分)或结束(后半部分)停止时,显示的值是预期的值(只完成了大约十二个测试)

关于javascript - 找到命运之轮的值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17837618/

相关文章:

javascript - 如何使用 jQuery 简单地删除非嵌套文本的字符?

java - resize() 方法被调用了多少次?

javascript - 发送带有字母数字 "From"的 Twilio SMS 会返回错误消息 'From' 需要电话号码

javascript - 如何在 VueJS 自定义过滤器结果上应用函数/运算符?

algorithm - 计算光子追踪中的像素值

algorithm - 节点的最优物理排序

algorithm - 找到没有。最后一分钟提出的请求

algorithm - graph - Graph 中的 Embedded 和 Topological 之间有什么区别?

javascript - Confluence - 使用 "/spaces/flyingpdf/pdfpageexport.action?pageId="作为按钮

javascript - 如何在运行时在 javascript 中创建对象的多个实例?