Hallo,
bei meinem Code, soll ein Ball mit dem linken, rechten und oberen Rand kollidieren und die Richtung wechseln.
Bei meinem jetzigen Code wird dies jedoch ignoriert und fliegt einfach durch.
Würde mich über jede Hilfe freuen.
Vielen Dank!
bei meinem Code, soll ein Ball mit dem linken, rechten und oberen Rand kollidieren und die Richtung wechseln.
Bei meinem jetzigen Code wird dies jedoch ignoriert und fliegt einfach durch.
Würde mich über jede Hilfe freuen.
Vielen Dank!
Code:
//Klasse Ball
public void reactOnBorder() {
double posX = position.getX();
double dirX = direction.getDx();
double posY = position.getY();
double dirY = direction.getDy();
double width = Constants.SCREEN_WIDTH;
posX += dirX;
posY += dirY;
if(posY < 0){
dirX = -dirX;
}
if(posX < 0){
dirX = -dirX;
}
if(posX < 0 || posX > width){
dirX = -dirX;
}
}
Code:
//Klasse Constants
public static final Integer SCREEN_WIDTH = 880;
public static final Integer SCREEN_HEIGHT = 750;
Code:
//Klasse Level
public void run() {
game.notifyObservers();
while (true) {
// if ballWasStarted is true, the ball is moving
if (ballWasStarted) {
// Call here the balls method for updating his position on the playground
ball.updatePosition();
// Call here the balls method for reacting on the borders of the playground
ball.reactOnBorder();
// Tells the observer to repaint the components on the playground
game.notifyObservers();
}
}
}