In my game I got the actor called Pim and an object called Balk. The balk is meant to jump on, so you can get higher in the game (look at it like doodle jump, but instead of jumping you just stand on it.) The problem is, that if you want to jump on it, and you are too close to the object, you go in it through the side:
http://i.imgur.com/3beuvPO.png
When you want to jump on it and you hit the side, you should bump off instead of going into it. Here are some codes I have:
This is my class called "beweging" (translated it means movement)
In the class pim I use the current function to check if he is falling down:
public class beweging extends Actor { public void act() { // No action required } public int vSpeed = 1; public int acceleration = 1; public int jumpSpeed = 12; public static final int speed = 5; public void moveRight() { setLocation ( getX() + speed, getY() ); } public void moveLeft() { setLocation ( getX() - speed, getY() ); } public boolean onGround() { Object under = getOneObjectAtOffset(0, getImage().getHeight()/2 , null); return under != null; } public void setVSpeed(int speed) { vSpeed = speed; } public void fall() { vSpeed+=1; // add gravity int dir=(int)Math.signum(vSpeed); // determine direction for(int step=0; step!=vSpeed; step+=dir) // for each pixel-step { setLocation(getX(), getY()+dir); // move if(getOneIntersectingObject(null)!=null) // check intersection { setLocation(getX(), getY()-dir); // resistance (step-back) vSpeed=0; // stopped break; // forces exit out of 'for' loop } } } }
private void checkFall() { if (onGround()) { setVSpeed(0); } else { fall(); } }