Hallo, ich baue gerade ein jump 'n' run und die Art, wie ich Wände gebaut habe, ist weder effizient noch zuverlässig. Ich weiß nicht, wie man Wände effizient baut.
Bei meiner methode habe ich Object des Interfaces Solid, welche nur die Methode getBounds() hat, und gucke, ob sich der Spieler innerhalb des Solid Objekt befindet. Wenn ja, drücke ich ihn raus. Nur klappt das nicht immer. Code:
Bei meiner methode habe ich Object des Interfaces Solid, welche nur die Methode getBounds() hat, und gucke, ob sich der Spieler innerhalb des Solid Objekt befindet. Wenn ja, drücke ich ihn raus. Nur klappt das nicht immer. Code:
Code:
public void pushOut(Solid b) {
Rectangle bounds = b.getBounds(x, y);
Rectangle tBounds = getBounds();
if (!bounds.intersects(tBounds))
return;
short bx = (short) bounds.x;
short by = (short) bounds.y;
short x = (short) tBounds.x;
short y = (short) tBounds.y;
boolean rOverlap = x < bx && x < bx + bounds.width && x + tBounds.width > bx + bounds.width;
boolean lOverlap = x < bx && x + tBounds.width > bx;
boolean dOverlap = y < by && y < by + bounds.height && y + tBounds.height > by + bounds.height;
boolean uOverlap = y < by && y + tBounds.height > by && y + tBounds.height < by + bounds.height;
if (uOverlap || dOverlap) {
verVelocity = 0;
if (uOverlap) {
this.y += by - tBounds.height - tBounds.y;
} else {
this.y += by - tBounds.y;
}
} else if (lOverlap) {
this.x += bx - tBounds.width - tBounds.x;
} else if (rOverlap) {
this.x += bx - tBounds.x;
}
}