我正在開發一個HTML5應用程序,我正在畫布上以一定的速度和每個動畫之間的特定超時畫出一系列圖像。同時發生多個JavaScript超時
能夠多次使用它我已經爲它做了一個功能。
var imgNumber = 0;
var lastImgNumber = 0;
var animationDone = true;
function startUpAnimation(context, imageArray, x, y, timeOut, refreshFreq) {
if (lastImgNumber == 0) lastImgNumber = imageArray.length-1;
if (animationDone) {
animationDone = false;
setTimeout(playAnimationSequence, timeOut, refreshFreq);
}
context.drawImage(imageArray[imgNumber], x, y);
}
function playAnimationSequence(interval) {
var timer = setInterval(function() {
if (imgNumber >= lastImgNumber) {
imgNumber = 0;
clearInterval(timer);
animationDone = true;
} else imgNumber++;
}, interval);
}
現在在我的主代碼中,每次使用正確參數的startUpAnimation它都能正常工作。 但是當我想以不同的間隔和速度同時在屏幕上同時繪製多個動畫時,它不起作用!
startUpAnimation(context, world.mushrooms, canvas.width/3, canvas.height - (canvas.height/5.3), 5000, 300);
startUpAnimation(context, world.clouds, 100, 200, 3000, 100);
現在顯示在正確的位置,兩者的動畫,但他們在我的案件5000超時和300間隔動畫無論是在第一個叫的間隔時間和超時,所以。
我該如何解決這個問題,讓他們都獨立玩?我認爲我需要把它變成一堂課或其他東西,但我不知道如何解決這個問題。在某些情況下,我甚至需要使用此功能同時顯示5個動畫。
任何幫助,將不勝感激!
你只得到了一個'imgNumber',一個'lastImgNumber',和一個'兩個動畫animationDone'變量。 – Bergi 2013-02-22 13:45:43
它應該如何動畫?每次調用只繪製一次,間隔只增加一個變量,但除此之外別無其他。 – Bergi 2013-02-22 13:46:46