This site requires JavaScript, please enable it in your browser!
Greenfoot back
Biscuits1
Biscuits1 wrote ...

2019/3/28

Moving the Character

Biscuits1 Biscuits1

2019/3/28

#
I want the character to move up,down,left,right with the assigned arrow keys. What is the coding for that?
IDK wagwan im new so ye
Nate2002 Nate2002

2019/3/28

#
Here's what I use:
if(Greenfoot.isKeyDown("down")) 
        {
            setLocation(getX(),getY()+5);
        }
That's for down Here's the up version:
if(Greenfoot.isKeyDown("up")) 
        {
            setLocation(getX(),getY()-5);
        }
Now you see the word "up" and "down" in quotation marks? If you want left and right as well change the words in quotation marks to any key you want. If you want left and right movement put the number in the get Y() into the getX(). Positive numbers is for down and right movement and negative numbers are for up and left movement
Chelrieo Chelrieo

2019/3/29

#
Here's what i use for left and right movement
if (Greenfoot.isKeyDown("left")) {
            velocityX = -4.6723;

        } else if (Greenfoot.isKeyDown("right")) {
            velocityX = 4.6723;          
        }
im not a pro but i hope it helps
Biscuits1 Biscuits1

2019/3/29

#
Helps a lot, thanks a lot!
You need to login to post a reply.