Reputation: 43
So it's the same drag and drop game project that I am working on, but I ran into a infinite loop problem, in which I want to use while(backpackLength<5), then the program will run. However, I have no idea where went wrongly, I couldn't even load my page, but I am sure that I am encountering with a infinite loop. Any suggestion?
var backpackLength = 0;
$(function() {
$("#cream").draggable();
$("#charger").draggable();
$("#doll").draggable();
$("#draft").draggable();
$("#folder").draggable();
$("#note").draggable();
while(backpackLength< 5) {
$("#bag").droppable({
drop: function() {
if (registerItem == 1) {
backpack.push("cream");
backpackLength++;
$("#cream").remove();
}
if (registerItem == 2) {
backpack.push("charger");
backpackLength++;
$("#charger").remove();
}
if (registerItem == 3) {
backpack.push("doll");
backpackLength++;
$("#doll").remove();
}
if (registerItem == 4) {
backpack.push("draft");
backpackLength++;
$("#draft").remove();
}
if (registerItem == 5) {
backpack.push("folder");
backpackLength++;
$("#folder").remove();
}
if (registerItem == 6) {
backpack.push("note");
backpackLength++;
$("#note").remove();
}
if (registerItem == 7) {
backpack.push("pens");
backpackLength++;
$("#pens").remove();
}
if (registerItem == 8) {
backpack.push("psp");
backpackLength++;
$("#psp").remove();
}
if (backpackLength > 0) {
document.getElementById("bag").src = "minigame/bag2.png";
}
if (backpackLength > 4) {
document.getElementById("bag").src = "minigame/bag3.png";
}
}
});
}
});
Upvotes: 2
Views: 54
Reputation: 611
if register item isn't equal to any of those things it will just keep running and running you need an else case that breaks the loop
Upvotes: 2