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

2012/9/12

Movement Angles Changing

Mikeson Mikeson

2012/9/12

#
This question has probly been answered however I carn't find it. Anyhow how do you change the direction of movement ? I.e In greenfoot the default forward run to the right of the page, however I would like to change it so it runs up the page (90 degrees it would be maybe considering the default settings). So in thoery when I press up the object moves up when the up key is pressed and vice vesa for down. Thanks in advance for your input.
Zamoht Zamoht

2012/9/12

#
If you use the move(int distance) you have to change the rotation of the object to make it move up. So it would look something like this.
public void moveUp()
{
     setRotation(270);
     move(1);
}
Zamoht Zamoht

2012/9/12

#
If you don't want your object to rotate, then this will work as well.
act()
{
     if (Greenfoot.isKeyDown("up"))
     {
          moveUp();
     }
}

public void moveUp()
{
     setLocation(getX(), getY() - 1);
}
Mikeson Mikeson

2012/9/12

#
Thanks for the reply Zamoht your second post is more of what I'm looking for except it makes the object spin on the spot when turning. I might just stick to the default settings and us the up down presses for speed.
Zamoht Zamoht

2012/9/12

#
Could you write your act method here ?
Gevater_Tod4711 Gevater_Tod4711

2012/9/14

#
There are two different methods in Greenfoot for turning your actor. The first is setRotation(int angel): this method turns your actor to the absolute angel which means that if you write setRotation(0) your actor turns to 0 degrees (to the right). The second it turn(int angel): this method turns your actor to the relative angel of the actor which means if your actor has the rotation 90 and you now say turn(5) the rotation will be 95 and if you say turn(0) nothing will hapen. If you want your actor to go up when the upKey is pressed you could use this:
if (Greenfoot.isKeyDown("up")) {
    setRotation(270);
}
else if (Greenfoot.isKeyDown("down")) {
    setRotation(90);
}
else if (Greenfoot.isKeyDown("left")) {
    setRotation(180);
}
else if (Greenfoot.isKeyDown("right")) {
    setRotation(0);
}
You need to login to post a reply.