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

2013/10/22

make a rocket move

smilyface999 smilyface999

2013/10/22

#
how can you make a rocket move using the arrow keys
Gevater_Tod4711 Gevater_Tod4711

2013/10/22

#
The easyest way to do this would be to use the arrows left and right to turn the rocket and up and down to move vorward and backward:
public void act() {
    if (Greenfoot.isKeyDown("left")) {
        turn(-5);
    }
    if (Greenfoot.isKeyDown("right")) {
        turn(5);
    }
    if (Greenfoot.isKeyDown("up")) {
        move(5);
    }
    if (Greenfoot.isKeyDown("down")) {
        move(-5);
    }
}

private void move(double distance) {
    double angle = Math.toRadians(getRotation());
    setLocation(getX() + (Math.cos(angle) * distance), getY() + (Math.sin(angle) * distance));
}
smilyface999 smilyface999

2013/10/22

#
cheers:)
You need to login to post a reply.