Hallo!
Ich habe eine Klasse Player und eine Klasse Ball. in der Klasse Ball soll die Position des Players abgefragt werden. Das problem ist, das die Position sich nicht aktualisiert und ich so immer nur die alte Position bekomme (350).
Was läuft da falsch?
Die Klasse Player:
Die Ball Klasse:
Vielen Dank.
Ich habe eine Klasse Player und eine Klasse Ball. in der Klasse Ball soll die Position des Players abgefragt werden. Das problem ist, das die Position sich nicht aktualisiert und ich so immer nur die alte Position bekomme (350).
Was läuft da falsch?
Die Klasse Player:
Java:
package com.ludevstudio.brickbreaker;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.security.Key;
import javax.swing.Action;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Player {
private int playerWidth = 100;
private int playerHeight = 10;
private Rectangle player;
public int playerPosX = 350;
private int playerPosY = 750;
public void draw(Graphics2D g) {
g.setColor(Color.WHITE);
g.fillRect(playerPosX, playerPosY, playerWidth, playerHeight);
}
public Rectangle getPlayer() {
player = new Rectangle(playerPosX, playerPosY, playerWidth, playerHeight);
return player;
}
public void moveLeft() {
if(playerPosX<10) {
playerPosX = 5;
} else {
playerPosX -= 30;
}
}
public void moveRight() {
if(playerPosX>790-playerWidth-5) {
playerPosX = 795-playerWidth-5;
} else {
playerPosX += 30;
}
}
}
Die Ball Klasse:
Java:
package com.ludevstudio.brickbreaker;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.lang.reflect.GenericArrayType;
import java.util.Random;
public class Ball {
int ballPosX = 390;
int ballPosY = 600;
int ballWidth = 20;
int ballHeight = 20;
int ballXdir;
int ballYdir;
Random random = new Random();
// needed Components
Border border;
Player player;
public Ball() {
border = new Border();
player = new Player();
ballXdir = 0; // random.nextInt(12)-6;
ballYdir = -3;
}
public void draw (Graphics2D g) {
g.setColor(Color.WHITE);
g.fillOval(ballPosX, ballPosY, ballWidth, ballHeight);
}
public Rectangle getBall() {
return new Rectangle(ballPosX, ballPosY, ballWidth, ballHeight);
}
public void move() {
ballPosX = ballPosX+ballXdir;
ballPosY = ballPosY+ballYdir;
}
public void rebound() {
if(getBall().intersects(border.getLeftBorder())) {
ballXdir = -ballXdir;
} else if(getBall().intersects(border.getRightBorder())) {
ballXdir = -ballXdir;
} else if(getBall().intersects(border.getTopBorder())) {
ballYdir = -ballYdir;
} else if(getBall().intersects(player.getPlayer())) {
ballYdir = -ballYdir;
playerRebound();
}
}
public void playerRebound() {
int boundPositon = (int) (player.getPlayer().getCenterX()-getBall().getCenterX());
System.out.println(player.getPlayer().x);
}
}
Vielen Dank.