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

2018/5/28

How Do I Make My Actor Move Forward?

Zweeg Zweeg

2018/5/28

#
I understand the different ways of standard movement in Greenfoot. To make your actor change its x-axis location, use the move() method. To make your actor change its y-axis location, use the setLocation() method. However, how do I make my actor move forward when it's facing different rotations? For example, a rocket moves forward and up when it's pointing up. It also moves forward when it's rotated to the side but this time forward is to the left or right and parallel to the ground. How do I do this same principle for my actor in Greenfoot?
danpost danpost

2018/5/28

#
The move method takes rotation into account when moving. An actor is initially at a facing-right rotation, so it will move right with the move method. It moves in the direction the actor is facing (if moving only one or two pixels at a time, it may be limited by the hardware -- that is, you cannot move between pixels on the screen). Anyway, move is not just for horizontal movement and setLocation is not just for vertical movement. You could track the horizontal and vertical speeds and add them to getX and getY to set new location coordinates for setLocation.
Zweeg Zweeg

2018/5/28

#
@danpost ok so I understand now that I can use the move method to move up and down too. But do you know how to incorporate the move method into a method that simulates the effects of gravity? Right now I have this method as my Gravity method but as you can see, using the move method would interfere with how the actor reacts to Gravity as I'm using yVelocity to change my vertical speeds. If I were to use the move method, it immediately moves in the opposite direction instead of slowing the downward motion before going back up. public void fall() { setLocation( getX() + xVel, getY()+yVel ); yVel += GRAVITY; }
danpost danpost

2018/5/28

#
Zweeg wrote...
do you know how to incorporate the move method into a method that simulates the effects of gravity?
Gravity only affects vertical speed, so using setLocation is probably better here. If you want to have the rotation to match the direction of movement, you can set it according to the values of xVel and yVel. Maybe this post can shed some light.
You need to login to post a reply.