我正在使用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函数,但似乎不起作用。任何帮助将不胜感激。感谢
答案 0 :(得分:2)
嗨伙计这是我需要的解决方案。我希望这会帮助别人......干杯
var dice_twos = new Array();
var dice_threes = new Array();
var dice_fours = new Array();
var dice_fives = new Array();
var dice_sixes = new Array();
var dice_1 = new Array();
var dice_2 = new Array();
var dice_3 = new Array();
var dice_4 = new Array();
var dice_5 = new Array();
var dice_6 = new Array();
var i = 0;
var n = 0;
var item1;
var item2;
var item3;
var item4;
var item5;
var item6;
for (var i = 1; i <= 50; i++) {
dice_twos.push({ src: "Assets/DiceImages/TWO_Main." + i + ".png", id: "dice" + i, reelImg: false });
dice_threes.push({ src: "Assets/DiceImages/THREE_Main." + i + ".png", id: "dice" + i, reelImg: false });
dice_fours.push({ src: "Assets/DiceImages/FOUR_Main." + i + ".png", id: "dice" + i, reelImg: false });
dice_fives.push({ src: "Assets/DiceImages/FIVE_Main." + i + ".png", id: "dice" + i, reelImg: false });
dice_sixes.push({ src: "Assets/DiceImages/SIX_Main." + i + ".png", id: "dice" + i, reelImg: false });
}
}
// Create a preloader. There is no manfest added to it up-front, we will add items on-demand.
preload = new createjs.PreloadJS();
preload.onFileLoad = handleFileLoad;
preload.setMaxConnections(5);
loadAll();
}
function stop() {
if (preload != null) { preload.close(); alert("preloader closed..."); }
}
function loadAll() {
while (dice_ones.length > 0) {
loadAnother();
}
}
function loadAnother() {
// Get the next manifest item, and load it
item1 = dice_ones.shift();
item2 = dice_twos.shift();
item3 = dice_threes.shift();
item4 = dice_fours.shift();
item5 = dice_fives.shift();
item6 = dice_sixes.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);
dice_1[n].in_holder = (-1);
dice_2[n] = new createjs.Bitmap(item2.src);
dice_2[n].name = ('dice' + (n + 1));
dice_2[n].regX = (diceRegX - 5);
dice_2[n].regY = (diceRegY - 5);
dice_2[n].x = (canvas.width + 80);
dice_2[n].y = (canvas.height / 2);
dice_2[n].in_holder = (-1);
dice_3[n] = new createjs.Bitmap(item3.src);
dice_3[n].name = ('dice' + (n + 1));
dice_3[n].regX = (diceRegX - 5);
dice_3[n].regY = (diceRegY - 5);
dice_3[n].x = (canvas.width + 80);
dice_3[n].y = (canvas.height / 2);
dice_3[n].in_holder = (-1);
dice_4[n] = new createjs.Bitmap(item4.src);
dice_4[n].name = ('dice' + (n + 1));
dice_4[n].regX = (diceRegX - 5);
dice_4[n].regY = (diceRegY - 5);
dice_4[n].x = (canvas.width + 80);
dice_4[n].y = (canvas.height / 2);
dice_4[n].in_holder = (-1);
dice_5[n] = new createjs.Bitmap(item5.src);
dice_5[n].name = ('dice' + (n + 1));
dice_5[n].regX = (diceRegX - 5);
dice_5[n].regY = (diceRegY - 5);
dice_5[n].x = (canvas.width + 80);
dice_5[n].y = (canvas.height / 2);
dice_5[n].in_holder = (-1);
dice_6[n] = new createjs.Bitmap(item6.src);
dice_6[n].name = ('dice' + (n + 1));
dice_6[n].regX = (diceRegX - 5);
dice_6[n].regY = (diceRegY - 5);
dice_6[n].x = (canvas.width + 80);
dice_6[n].y = (canvas.height / 2);
dice_6[n].in_holder = (-1);
n++;
}