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

2024/1/31

Collision problem in a platformer game

Parrot279 Parrot279

2024/1/31

#
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).
You need to login to post a reply.