RGB

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

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?

Share this post


Link to post
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') {
}

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.