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

2013/6/26

Gravity

How do you add gravity? also how do you code platforms? The DoctorProfessor is out!
Gevater_Tod4711 Gevater_Tod4711

2013/6/26

#
Using gravity is not very difficult. You just have to declare a variable gravity in the worldclass or anywhere you want to use it and add the value of this gravity variable to the actors current y movement:
private double velY;//the actors y movement;

public void act() {
    velY += ((YourWorld) getWorld().GRAVITY;//to use the gravity like this you should declare it public static final;
    //... some other stuff;
    setLocation(getX() + velX, getY() + velY);
}
Then you can add some things like a maximum falling speed or that the y movement is 0 if your actor is standing on a platform. To create platforms I would advice you to use Gravity Engine by Busch2207. Using this demo you can create platforms your actor can stand on.
sametguzelgun sametguzelgun

2013/6/26

#
http://www.greenfoot.org/scenarios/7586
Tried putting your code into the world class, and it didn't work. Tried it in the actor class, and it said that
cannot find symbol - variable GRAVITY
Do you have any ideas what's going on? Oh and @sametguzelgun, I couldn't understand what your code meant. I only speak English, a small amount of Spanish, even less French, Klingon, Goa'uld, Jabberwocky (Wookie), Ewok and Huttese. please post all code in those languages. The DoctorProfessor is out!
danpost danpost

2013/8/3

#
I use gravity in both my Scrolling SuperWorld and my Toll-Tally Mad scenarios with open code.
Gevater_Tod4711 Gevater_Tod4711

2013/8/4

#
If your actor doesn't find the variable GRAVITY you propably declared it private or tried to access it the wrong way. I think GRAVITY should be a variable in the world (declared public static final). If you want to access this variable from your actor you can't use just the name of the variable but the classname of your world and the variable name: WorldName.GRAVITY; If this is not the problem you should post the code that doesn't work here.
You need to login to post a reply.