Jump to content

I want one instruction to run at once only in this expected manner but not happening.


RGB
 Share

Recommended Posts

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

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

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...