Ok, so in my Java2D Game, using swing/awt (I hear it is not the best to combine those), I am trying to finish collisions.
So, the problem is, the "if (player.xvel < 0) { }" and the "if (player.xvel > 0) { }" in the Block class are still being executed even though the player hasn't run into the block horizontally. How can I make it so the code is executed when the player runs/walks into the block horizontally?
My code for the collide function is below, in Block.java:
void collide(Player player) {
Rectangle player_hb = new Rectangle((int) player.x, (int) player.y, (int) player.width, (int) player.height);
Rectangle block_hb = new Rectangle(x + Display.graphics_panel.WorldX, y, width, height);
if (player_hb.intersects(block_hb)) {
Rectangle intersection = (Rectangle) player_hb.createIntersection(block_hb);
if (player.yvel > 0) {
player.jumping = false;
player.y -= intersection.getHeight();
}
if (player.yvel < 0) {
player.yvel = 0;
player.y += intersection.getHeight();
}
/* Execute this code ONLY if player is intersecting a block HORIZONTALLY!! */
if (player.xvel > 0) {
System.out.println("You shouldn't see this unless you run into a block!" + i++);
}
if (player.xvel < 0) {
System.out.println("You shouldn't see this unless you run into a block!" + i--);
}
}
}