I'm trying to make a class method in JS where an action have to be repeated only once. The method is call in a loop, and I want it method to move a character once and stop. But the click event is repeated a lot, so the character move ~40 times, and when I try with the jQuery "one()" it doesn't works, it's the same.
So that is my function (called in a permanent loop) :
Player.prototype.pushBox = function() {
$(".box").click(function() { // The 'one()' doesn't resolve anything
console.log('coucou'); // This is printing 40 times
for (var i = 0; i < boxArray.length; i++) {
var box = boxArray[i];
if (box.id == player.id + moveHorizontal) {
$("#tile" + box.id).addClass('ground');
box.x += boxSize;
box.id += moveHorizontal;
player.x += boxSize; // So this is called 40 times
player.id += moveHorizontal;
}
}
});
}
Aucun commentaire:
Enregistrer un commentaire