RGB Posted September 24, 2016 Share Posted September 24, 2016 for(i=0;i<arr.length;i++) { a=index of i in arr; if(a=="moveRight") { image.body.velocity.x=150; } if(a=="moveDown") { image.body.velocity.y=150; }} when arr[0]=='moveRight' and arr[1]=='moveLeft' it looks like the image moves diagonally but I want only one to work at once. What should I do here now? Link to comment Share on other sites More sharing options...
rgk Posted September 24, 2016 Share Posted September 24, 2016 your looking for a switch or elseif, the code is allowing both moveRight and moveLeft to be triggered, if you wrote it like this you will be good. switch (a) { case 'moveRight': case 'moveLeft': case 'moveUp': case 'moveDown': }; or if (a == 'moveRight') { } else if (a == 'moveLeft') { } else if (a == 'moveUp') { } else if (a == 'moveDown') { } Link to comment Share on other sites More sharing options...
Recommended Posts