2012-12-22 4 views
-5

표시 할 주사위를 결정하기 위해 셔플 기능을 호출하는 yatzy-game을 구축하고 있습니다.주사위 애니메이션 jQuery

$("#dice" + i).attr('src', 'img/Dice' + yatzyLogic.randomDice() + '.gif'); 

그것은 잘 작동하지만, this 같은 것을 주사위를 던질 때 나는 애니메이션을합니다. 그러나 이것을 적용하면 수십 개의 코드를 다시 작성해야하고 저는 게으르다. :)

내 코드에 그런 종류의 것을 적용하는 좀 더 쉬운 방법이 있습니까? 다른 이미지를 빠르게 보여주는 타이머 기능처럼?

+2

게으른 경우 지금 작성해야하는 코드를 작성하기 전에 생각해야합니다. 리팩토링 – charlietfl

+0

글쎄, 나는 초보자지만 나중에 들려 .. – bangerang

+0

만약 당신이 코드의 톤을 재작 성해야한다면 * 아주 아주 * 코드에 문제가있다. 이 플러그인에는 매우 쉽고 편안한 API가 있습니다. 방금 함수를 호출하고 주사위 굴림 결과를 콜백으로 처리하는 함수를 전달합니다. – ThiefMaster

답변

0

http://pannonicaquartet.com/test/ej_010_R.html (404)

<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); 
} 
} 
+0

고마워요 @HMarioD – bangerang

0

은 각 주사위 값 애니메이션 GIF 비 반복 생성 할 수 (그 최종 프레임 당연히 주사위 값 것이다 ) ...

하지 당신이 정말로 이것을해야한다고 말하자면 (나는 실제로 당신이이 아니라고 제안하고있다. 단지 속임수 일 뿐이다.
그러나 현재 코드를 다시 코딩하거나 다시 팩할 수 없다고 말하면 다른 경로를 볼 수 없습니다.