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
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 { and }
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.
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.
Rotations in Greenfoot:
3. Code the Other Arrow Keys
Directly underneath the if statement for turning and moving right, add the code for turning and moving down.
You can see in the diagram above the degrees to rotate in each of the four directions.
Write the code to move in all four directions.
​
Ensure you have the correct number of brackets or the program won't start.
​
Remember brackets that start and end on the same line are ( ) and brackets over multiple lines are { }.
4. Compile and Run
Click the Compile button at the top of the code editor.
​
Then you can go back to the main Greenfoot window and click Run.
​
Press the arrow keys to test your main character moves.