Ho un'immagine di una ruota della ruota della fortuna e sto cercando di fare in modo che quando gira mostra l'ammontare corretto per quello che è stato ruotato.Trova il valore sulla ruota per la ruota della fortuna
Ho il codice seguente: http://jsfiddle.net/maniator/rR67s/
Molte volte è corretto, ed altro momento è sbagliato.
Per esempio mi sono girato questo:
Ed è avvisato , che è sbagliato.
Come posso correggere il mio algoritmo in modo che sia corretto il 99% delle volte (o 100% se possibile)?
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)
});
gioco completo per chi vuole provare: http://jsfiddle.net/maniator/XP9Qv/ (< - questo è stato aggiornato utilizzando risposta accettata)
Il divertimento continua here.
Pensi di essere bello con la tua fantasia di gioco della Ruota della fortuna? Bene, è bello ... :) – Ian
@Ian Grazie^_^Ho aggiunto un link nella parte inferiore del mio OP al gioco completo in cui viene utilizzato il 'Wheel'. – Neal
Credo che l'errore abbia a che fare con il javascript che determina il risultato prima che la ruota si fermi completamente, come sembra quando si verifica l'errore. Vedrò se riesco a individuare la causa esatta. – Duane