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

2020/10/30

how to do gravity?

WINER8 WINER8

2020/10/30

#
does somebody can create gravity? if yes please told me how to do it
danpost danpost

2020/10/30

#
WINER8 wrote...
does somebody can create gravity? if yes please told me how to do it
Please refer to my Jump and Run Demo w/Moving Platform scenario.
RcCookie RcCookie

2020/10/30

#
Say you have a class Player. This class should extend SmoothMover, a Greenfoot class that uses floating-point variables to store the actors position. You can import it by right-clicking onto the panel with „Actor“ in Greenfoot and selecting „Import“.
public class Player extends SmoothMover {
    public void act() { }
}
To store the current speed of the player we create a double variable.
public class Player extends SmoothMover {
    // vY stands for velocity in Y direction
    private double vY = 0;
    ...
Now every frame the player needs to move that speed.
public void act() {
    // getExactX/Y are methods from SmoothMover that return the location as double
    setLocation(getExactX(), getExactY() + vY);
}
Now also every frame the vertical speed has to increase by a certain amount which is the effect of gravity.
...
private static final double GRAVITY = 0.1;
public void act() {
    vY += GRAVITY;
    ...
WINER8 WINER8

2020/11/1

#
thank you danpost and RcCookie
WINER8 WINER8

2020/11/1

#
RcCookie when i was writing the code "setLocation(getExactX(), getExactY() + vY);" the greenfoot inform me there are not method getExactX() and method getExactY() can you help me please
danpost danpost

2020/11/1

#
WINER8 wrote...
RcCookie when i was writing the code "setLocation(getExactX(), getExactY() + vY);" the greenfoot inform me there are not method getExactX() and method getExactY() can you help me please
RcCookie wrote...
Say you have a class Player. This class should extend SmoothMover, a Greenfoot class that uses floating-point variables to store the actors position. You can import it by right-clicking onto the panel with „Actor“ in Greenfoot and selecting „Import“.
Although, I am not sure about the instructions given to import it. I know of using the menu bar with : Edit >> Import class...
You need to login to post a reply.