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

2014/10/16

gravity on my platformer not working right..

bmw0045 bmw0045

2014/10/16

#
I'm starting to make a platformer and I cannot get the gravity to work right, if the block spawns below him, he will levitate above it and be able to move side to side, but if he "falls" onto it from the start, he falls until he hovers about 10 pixels above it. Also, if he is "standing"(leviatating) on the platform, he doesnt fall down when he steps off of it. Any help? public void checkFall() { if (onGround()){ gravity = 0; acceleration = 0; } else { fall(); } } public boolean onGround() { int spriteHeight = getImage().getHeight(); Actor ground = getOneObjectAtOffset(0, getImage().getHeight()/2, Platforms.class); if(ground == null) { return false; } else { moveToGround(ground); return true; } } public void moveToGround(Actor ground) { int groundHeight = ground.getImage().getHeight(); int newY = ground.getY() - (groundHeight + getImage().getHeight())/2; setLocation(getX(), newY); }
danpost danpost

2014/10/20

#
Chances are one or more of the images involved (his image, the ground image or both) have excess rows of transparent pixels that not actually part of the image portrayed, but still is part of the rectangle that makes up the image block. If that is the case, the images blocks will intersect before the actual images intersect which will cause the behavior you described. You can use an image editor (one that supports transparency) to remove any complete rows (or columns) along the edges of your image that are completely transparent. If you do not have an image editor the supports transparency, you can download my Image Transparency Adder/Trimmer scenario and run it -- load and save your image file(s) and the excess transparency is automatically removed. You can even create an executable jar file from it and then use it.
You need to login to post a reply.