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

2013/4/27

how do you control Clara the ladybug to move, turn right, turn left, turn around by clicking on arrow keys?

gquach gquach

2013/4/27

#
how do you control Clara the ladybug to move, turn right, turn left, turn around by clicking on arrow keys? I was given a command Greenfoot.isKeyDown( String )and setDirection( String ), and move( int ) how do control the ladybug to move accordingly by clicking on arrow keys?
Gevater_Tod4711 Gevater_Tod4711

2013/4/27

#
To check whether a key is down you can use Greenfoot.IsKeyDown(String keyName). The keynames are the letters ("q", "w", "e", "r", ...) or for some special keys you need the name of this keys like "space" or "up", "down", ... To make your actor turn you can use the setDirection method but you better use the turn(int amount) method. This method makes your character turn a degree value you can add in the brakets. A positive value makes your actor turn clockwise and a negative value anticlockwise. To make your actor move into the direction he's looking to you use the method move(int distance). That will make the actor move the distance you want to. The distance is counted cells.
danpost danpost

2013/4/27

#
@Gevater_Tod4711, I believe that Clara is in a GridWorld layout where only 'up', 'right', 'down' and 'left' are valid directions; so 'turn(int)' is not something that will be used. @gquach, you will probably need a timer field to regulate the movement (so Clara does not just 'jump' to the edge of the world). Use a value somewhere between 15 and 40 (whatever seems natural when running the scenario). If the timer has elapsed and only one of the four keys are pressed, turn and move in that direction. These conditions will need checked for each valid directional keypress.
You need to login to post a reply.