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

2013/9/20

Help

Mishti Mishti

2013/9/20

#
I am having trouble getting my whale to turn back and go to the middle of the world and stay there from the bottom right corner.
Gevater_Tod4711 Gevater_Tod4711

2013/9/20

#
If your actor is at the botom of the world and you want it to move to the middle of the world you can use the methods turnTowards(int, int) and move(). TurnTowards is a method in the API so your actor already knows this method. If you have no move method you can use this one:
public void move(double distance) {
        double angle = Math.toRadians(getRotation());
        int x = (int) Math.round(getX() + Math.cos(angle) * distance);
        int y = (int) Math.round(getY() + Math.sin(angle) * distance);
        setLocation(x, y);
    }
Now you just need to make your actor turn and move in the direction you want:
//in your actor subclass;
public void act() {
    if (getX() > getWorld().getWidth()/2 - 5 && getX() < getWorld().getWidth()/2 + 5 && getY() > getWorld().getHeight()/2 - 5 && getY() < getWorld().getHeight()/2 + 5) {//make your actor stop in the middle of the world;
        turnTowards(getWorld().getWidth()/2, getWorld().getHeight()/2);
        move(3);
    }
}
Mishti Mishti

2013/9/23

#
Thank you!
You need to login to post a reply.