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

2016/6/14

Turn in the direction its moving?

coder04 coder04

2016/6/14

#
        setRotation(90);
if (Greenfoot.isKeyDown("up"))
  { move(-4);
    turn(90);}
 
if (Greenfoot.isKeyDown("down"))
  { move(4); }
setRotation(0);
if (Greenfoot.isKeyDown("left"))
{
move(-3);
}

if (Greenfoot.isKeyDown("right"))
{
move(3);
}
coder04 coder04

2016/6/14

#
So the object faces the way its moving in
danpost danpost

2016/6/14

#
coder04 wrote...
So the object faces the way its moving in
I understand what you want as far as the movement direction being that of the rotation. What I do not understand is what you want the up/down and left/right keys to be doing. Maybe you could also explain what directions you with to allow the actor to move. For example, along only the horizontal and vertical; maybe along the diagonals also, or in any one of the possible 360 degree angles that the rotation of the actor can be in.
coder04 coder04

2016/6/14

#
Ah So when you press the up key the actor moves forward but the actor does not turn to face that direction aswell. I tried using the turn(90) but nothing happened
danpost danpost

2016/6/14

#
coder04 wrote...
Ah So when you press the up key the actor moves forward but the actor does not turn to face that direction aswell. I tried using the turn(90) but nothing happened
First, it is strange to see the vertical movement speed ( 4 ) being faster than the horizontal movement speed ( 3 ). Next, with the current code, by not using 'else if' structures, you will are allowing diagonal movement as well. Unfortunately, even though diagonal movement are avoided, some directions will supersede others with that. There are other ways to code key controls that avoid diagonal movement and avoid one key take precedence over another. Finally, the only code that sets a specific rotational value is at line 8. However, it is done unconditionally which means if you set any rotation prior to it -- forget it; it will be returned back to a rotation of zero (or no rotation). If you want to set specific rotation depending on the key (which determines the direction the actor is to move), then it needs to be done at the prior to moving inside the check for that specific key. Also, notice I am saying to "set the rotation" -- not to "turn" one way or another.
You need to login to post a reply.