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

Report as inappropriate.

Fishwaffles
Fishwaffles presents ...

2011/10/26

first game

I need help adding gravity to my game. Eventually I'd like for it to be able to jump and come back down.

1991 views / 8 in the last 7 days

Tags: game physics with-source gravity help

open in greenfoot
Your browser does not support the canvas tag.
limefortheworldlimefortheworld

2011/10/26

You're using the move(int b) function. Essentially, it moves you some pixels (assuming that the cell size is 1 pixel) towards the direction you are facing. If you want gravity, move function just won't cut it: You will need to use the setLocation(int x, int y) function, and with gravity, you will need to store some integer value (if you are not going to use the smooth mover, although using that is highly recommended if you want good smooth jumps, but then you will need to use doubles or floats, but neither one don't work with the setLocation function by itself something that can be somewhat mitigated by typecasting) to store your y-speed value. It'd look something like this private int gravpull = 0; public void act() { if(notTouchingGround()) { gravpull ++; setLocation(getX(), getY()+gravpull); } } public boolean notTouchingGround() { blah blah blah, although in this case you can settle for return getY() < (some height); } Hope that helps.
limefortheworldlimefortheworld

2011/10/26

and remember to reset and perhaps cap the gravity variable, because not resetting the variable will lead it to plummet unnaturally down when not on the ground. (a constant for the acceleration, and a variable for the actual movement.)
FishwafflesFishwaffles

2011/10/29

thanks I'll try it when i get a chance.

Want to leave a comment? You must first log in.

Who likes this?

No votes yet.