I am currently making a small scrolling canvas game, and I am trying to implement monsters. I keep two arrays for the monsters, one for their x positions and one for their y positions. For some unexpected reason, both of the arrays get filled up to 1152 elements, two of them are predefined, and the others are all "NaN".
Here's a link to the program: http://codepen.io/kenshin791011/pen/WpxQmg
(The function monsterMove is run on key down)
var monsterx = [13, 5],
monstery = [41, 42];
function monsterMove() {
for(var i = 0; i < monsterx.length; i++) {
var z = Math.floor(Math.random() * 4);
switch(z) {
case 0:
monsterx[i] -= 1;
break;
case 1:
monstery[i] -= 1;
break;
case 2:
monsterx[i] += 1;
break;
case 3:
monstery[i] += 1;
break;
}
}
}
monsters = [{x:13,y:41}, {x:5,y:42}]? Just seems neater to me.