gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 04:42:37 25 4
gpt4 key购买 nike

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

我有以下代码: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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com