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

Discussions

You need to login to take part
Rss

Current Discussions

Parrot279

Collision problem in a platformer game

By Parrot279, with no replies.
Hello, I'm new in programming in greenfoot and I've encountered a problem when coding the collision method. When meeting an obstacle, my actor only phases through the upper part of the obstacle. These are my methods used to detect obstacles, respectively if the actor is on the ground: public void detectObstacole() { int dx = 0; int dy = 0; // checking for collision on the right side while (getOneObjectAtOffset(getImage().getWidth() / 2 + 1 + dx, 0, Platobstacole.class) != null) { dx--; } // checking for collision on the left side while (getOneObjectAtOffset(-getImage().getWidth() / 2 - 1 + dx, 0, Platobstacole.class) != null) { dx++; } // checking for collision on the top side while (getOneObjectAtOffset(0, -getImage().getHeight() / 2 - 1 + dy, Platobstacole.class) != null) { dy++; } // adjusting the position setLocation(getX() + dx, getY() + dy); } public boolean pePamant() { //checking whether the actor is on ground int x = getX(); int y = getY(); // checking for collision at current position if (isTouching(Platforme.class)) { return true; } setLocation(x, y + vitezaVerticala);// checking for collision at next position boolean onGround = false; if (isTouching(Platforme.class)) { onGround = true; } setLocation(x, y); // returns to former position return onGround; } As a note, the "Platforme" (which only includes platforms) class is a subclass of "Platobstacole" (which includes both platforms and obstacles).
1
2
3
4
5