我正在用 HTML 5 和 JS 制作一个骰子游戏。现在我需要预加载骰子图像。我正在使用 preloadJS 库。所以我的问题是如何在不为每个骰子编写相同代码的情况下预加载两个或多个清单?请看我的例子。
我的清单文件是:dice_twos、dice_threes、dice_fours、dice_fives、dice_sixes。 *一个骰子有 50 张图片*
var n=0;
var diceRegX = 45;//X registration point for dice
var diceRegY = 45;//Y registration point for dice
for(var i=0; i<50;i++){
dice_twos.push({ src: "Assets/DiceImages/ONE_Main." + i + ".png", id: "dice" + i, reelImg: false });
dice_threes.push({ src: "Assets/DiceImages/ONE_Main." + i + ".png", id: "dice" + i, reelImg: false });
dice_fours.push({ src: "Assets/DiceImages/ONE_Main." + i + ".png", id: "dice" + i, reelImg: false });
dice_fives.push({ src: "Assets/DiceImages/ONE_Main." + i + ".png", id: "dice" + i, reelImg: false });
dice_sixes.push({ src: "Assets/DiceImages/ONE_Main." + i + ".png", id: "dice" + i, reelImg: false });
}
preload = new createjs.PreloadJS();
preload.onFileLoad = handleFileLoad;
preload.onProgress = handleOverallProgress;
preload.onFileProgress = handleFileProgress;
preload.onError = handleFileError;
function stop() {
if (preload != null) {
preload.close();
}
}
function loadAll() {
while (dice_twos.length > 0) {
loadAnother();
}
}
function loadAnother() {
var item1 = dice_twos.shift();
handleFileLoad(item1);
}
// File complete handler
function handleFileLoad(event1) {
dice_1[n] = new createjs.Bitmap(event1.src);
dice_1[n].name = ('dice' + (n + 1));
dice_1[n].regX = (diceRegX - 5);
dice_1[n].regY = (diceRegY - 5);
dice_1[n].x = (canvas.width + 80);
dice_1[n].y = (canvas.height / 2);
n++;
}
我试图将另一个参数传递给 handleFileLoad 函数,但似乎不起作用。任何帮助将不胜感激。谢谢