javascript - Nested Loop is skipped within Function -


in basic html/javascript game, when moving character want adjust ai movement speed/position relative character's, not canvas. code works fine whichever key's function listed last.

//left button if (37 in keysdown) {   if (robot.x <= 400) {      //this statement runs     (var = 0; < boxes.length; i++) {         if (boxes[1].x < 0) {             boxes[i].x += robot.speed * modifier;             robot.x = 400;              //this statement problem, doesn't run @             (var j = 0; j < cogs.length; j++) {                 cogs[j].speedx = cogs[j].prevspeedx + robot.speed;             }            };      };   };    //this runs   robot.x -= robot.speed * modifier; } else {   (var j = 0; j < cogs.length; j++) {       cogs[j].speedx = cogs[j].prevspeedx;   } };  

and entire right button runs, because comes after left key:

//right button if (39 in keysdown) {   if (robot.x >= canvas.width - 400) {     (var = 0; < boxes.length; i++) {         if (boxes[1].x > -3000 + canvas.width) {             boxes[i].x -= robot.speed * modifier;             robot.x = canvas.width - 400;              //this identical above, robot.speed subtracted             (var j = 0; j < cogs.length; j++) {                 cogs[j].speedx = cogs[j].prevspeedx - robot.speed;             }         };      };   };   robot.x += robot.speed * modifier; } else {     (var j = 0; j < cogs.length; j++) {       cogs[j].speedx = cogs[j].prevspeedx;     } }; 

as far can tell isn't issue variables or values, because when switch order of right/left functions, left begins working , right stops.

full code: http://codepen.io/anon/pen/nqxbbx

edit: clarify, issue not code doesn't work when press left arrow (37), player , box movement portions fine. (keysdown object not array) only section affecting cog movement when player moving left is not working. virtually identical code runs when right key pressed works intended ... why it's been driving me mental.

i'll take wild guesses @ may solve issue!

the problem doesn't exist in for loops, rather if else structure.

you close answer when noticed whatever on bottom ran fine, on top didn't. because if top if statement caught, bottom else negates movement of cogs. doing following (which still has bugs, mind you), work:

http://codepen.io/anon/pen/zgqpzw

one thing noticed if robot runs wall, cogs keep on speeding though "box" isn't.

as aside, it's form not end if else statements semicolons since they're unnecessary.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -