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

2015/12/27

move in the other direction atWorldEdge without turn()

Ramin Ramin

2015/12/27

#
I want my object (Man) to move not only left and right but up and down as well. This has to happen randomly. I did it with move() and with setLocation( getX(), getY()+1). It works but I want them to move in the other direction when they are atWorldEdge. How can I do this without the turn( ) command? Because if I set the turn angle for example 180° then the object moves upside down in the other direction. But a Man walking upside down looks weird. I want him to move like mirrored in the other direction. Ich hab ein Objekt (Mann), welches ich sich nicht nur zufällig nach rechts und links, sondern auch nach oben und unten bewegen soll. Dies hab ich mit den Befehlen move() und setLocation( getX(), getY()+1) gemacht. Wen er sich nun am Rand der Welt umdrehen soll, kann er das ja mit turn(). Wenn ich den Winkel aber dann z.B. auf 180 Grad setze, geht der Mann dann aber auf dem Kopf stehend in die andere Richtung. Wie schaffe ich es, dass er das nicht tut, sondern sozusagen normal in die andere Richtung geht? Sozusagen sich spiegelverkehrt bewegt.
danpost danpost

2015/12/27

#
I think you need to give a little more information. With what you have given, it is hard to determine whether you want 4-way, 8-way or universal movement. 4-way would be moving horizontally and vertically, one or the other at any time. 8-way would be similar, except that included would be diagonal movement. Universal would be moving at any possible angle (which is still moving horizontally and/or vertically).
Ramin Ramin

2015/12/27

#
I think I want 8-way movement but without any turn() because I want my object walking like a real person and not with his head ahead... if you know what I mean...
danpost danpost

2015/12/28

#
An easy way for 8-way movement is to add an instance int field to track the current direction. Let its value be from zero to seven depending on the current direction (times 45 degrees) that is should travel. Then you would have:
1
2
3
4
5
6
7
// instance field
private int direction;
 
// movement code
setRotation(direction*45);
move(2); // adjust speed as needed
setRotation(0);
You need to login to post a reply.