Javascript 骰子动画jQuery
我正在构建一个yatzy游戏,在这里我调用一个洗牌函数来决定要显示哪个骰子Javascript 骰子动画jQuery,javascript,jquery,animation,Javascript,Jquery,Animation,我正在构建一个yatzy游戏,在这里我调用一个洗牌函数来决定要显示哪个骰子 $("#dice" + i).attr('src', 'img/Dice' + yatzyLogic.randomDice() + '.gif'); 它工作得很好,但我想要一个动画,当你掷骰子的时候,比如。但是如果我应用这个,我必须重新编写几十个代码,我很懒: 有没有更简单的方法将这种东西应用到我的代码中?像快速显示不同图像的计时器功能一样?404 您可以为每个骰子值创建非循环动画GIF,其最后一帧当然是骰子值 我不是
$("#dice" + i).attr('src', 'img/Dice' + yatzyLogic.randomDice() + '.gif');
它工作得很好,但我想要一个动画,当你掷骰子的时候,比如。但是如果我应用这个,我必须重新编写几十个代码,我很懒:
有没有更简单的方法将这种东西应用到我的代码中?像快速显示不同图像的计时器功能一样?404
您可以为每个骰子值创建非循环动画GIF,其最后一帧当然是骰子值 我不是说你真的应该这样做,而是建议你不要这样做,因为这只是一个骗局。。
但是如果你说你不能/不愿意重新编码/重新考虑你当前的代码,那么我就看不到另一条路了。懒惰,你应该在写代码之前考虑一下。你现在必须重构。好吧,我是个新手,但我听到了。如果你必须重写大量代码,那么你的代码就会出问题。该插件有一个超级简单和舒适的API。您只需调用该函数,并将处理掷骰子结果的任何函数作为回调传递。
<script language="javascript" type="text/javascript">
var spnResultado, imgDado, numero, timerReference, timeoutReference;
function init(){
try{
imgDado = document.getElementById("imgDado");
spnResultado = document.getElementById("spnResultado");
}catch(er){
if(console.log)console.log("Error in init: " + er);
if(spnResultado)spnResultado.innerText = "Error in init: " + er;
}
}
function btnStartOnClick(){
try{
if(!timerReference){
numero = Math.floor(Math.random()*6) + 1;
timerReference = setInterval(timerFunction,13);
var rnd = Math.floor(Math.random()*901)+800;
setTimeout(timeoutFunction,rnd);
spnResultado.innerText = "rnd " + rnd;
}
}catch(er){
if(console.log)console.log("Error in btnStartOnClick: " + er);
spnResultado.innerText = "Error in btnStartOnClick: " + er;
}
}
function timerFunction(){
try{
numero++;
if(numero > 6) numero = 1;
var offsetImagen = -(numero-1) * 80;
imgDado.style.marginLeft = offsetImagen + "px";
}catch(er){
if(console.log)console.log("Error in fnTimer:\n" + er);
}
}
function timeoutFunction(){
try{
clearInterval(timerReference);
timerReference = null;
spnResultado.innerText = "Número: " + numero;
}catch(er){
if(console.log)console.log("Error in fnTimer:\n" + er);
}
}