lundi 29 décembre 2014

Why is my .bind not calling my function in javascript


!!! Found out that .apply does work. Consider this question solved.!!!


I know, by calling console.log, that my anime function in my animate function is being called repeatedly, like it should. I know that both my if statements in my anime function work properly by calling console.log, so my .bind should call my other functions, unfortunately it does not for some reason. My third function is my animate function, and the two functions that are being called through .bind are above it. I do have a blocks array, and it works properly and the blocks[i] does work properly, but that code is not posted to to keep out unnecessary details. I do have a KeyBlock constructor and objects created from it, but that code is also not posted for the same reason. My question is just why my .bind is not working in my if statements (the if statements work) in my anime function in my animate function. I need my .bind because my blocks[i] needs to be the this, when passed to another function from those functions that .bind calls.



KeyBlock.prototype.actorMove = function() {
var plusMinus = Math.floor(Math.random() * 2);
var plusMinus2 = Math.floor(Math.random() * 2);
if(plusMinus == 0)
var xNew = -1;
else
var xNew = 1;
if(plusMinus2 == 0)
var yNew = -1;
else
var yNew = 1;
KeyBlock.prototype.move.bind(this, xNew, yNew);
}
KeyBlock.prototype.playerMove = function() {
addEventListener("keydown", function(event) {
if(event.keyCode == 37)
xNew = -1;
if(event.keyCode == 38)
yNew = -1;
if(event.keyCode == 39)
xNew = 1;
if(event.keyCode == 40)
yNew = 1;
KeyBlock.prototype.move.bind(this, xNew, yNew);
event.preventDefault();
});
}
KeyBlock.prototype.animate = function() {
var time = Date.now();
var anime = function() {
for(var i = 0; i < blocks.length; i++) {
if((blocks[i]).type == "player") {
KeyBlock.prototype.playerMove.bind(blocks[i]);
} else {
KeyBlock.prototype.actorMove.bind(blocks[i]);
}
}
requestAnimationFrame(anime);
}
requestAnimationFrame(anime);
}'


thanks ahead of time :)





Aucun commentaire:

Enregistrer un commentaire