How do i code directional keys into greenfooy

Right-click on your main character class and select ' Open editor '.

The editor allows you to write different methods - actions that the class can perform.

The act() method will repeat whenever the Run button is pressed.

1. Open the Code Editor

2. Movement with the Arrow Keys

Greenfoot Tutorial

gf1.PNG

2. Copy the Code CAREFULLY

You need to use an if statement to check if a certain key (like the right arrow key) is being pressed down .

An if statement must be contained in standard brackets . After each if statement, the proceeding code must be typed within curly brackets - see the image on the left .

Tip - If the brackets are on the same line then use the standard brackets ( and )

If the brackets are on different lines then use curly brackets

gf2.PNG

Your code must be perfect or it won't work. ' Greenfoot ' requires a capital G and the isKeyDown method most be written with a lowercase i but uppercase K and D .

gf3.PNG

When the right arrow key is pressed the object will change its rotation to 0° which is right . It will also move 1 place in this direction.

greenfoot17.png

Rotations in Greenfoot: