I created a camera class to be able to move around the map with the arrow keys. The background image is larger than the game window and when I move the camera around, the image is constantly being replaced and it basically destroys the background image. I uploaded a video so you can understand what’s happening:
The camera class only has x and y as variables with it’s setters and getters. It’s moving methods are. I show only left here, but I have the same for right, up and down (with the right coordinates being updated).
public void moveLeft() {
x = x-10;
}
Now, on the main class i’m updating it like this, the camera was initialized at (0,0):
@Override
public void run() {
while (true) {
builder.update();
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void update(Graphics g) {
if (image == null) {
image = createImage(this.getWidth(), this.getHeight());
second = image.getGraphics();
}
Graphics2D g2d = (Graphics2D) g;
second.setColor(getBackground());
second.fillRect(0, 0, getWidth(), getHeight());
second.setColor(getForeground());
g2d.translate(camara.getX(),camara.getY());
paint(second);
g.drawImage(image, 0, 0, this);
g2d.translate(-camara.getX(),-camara.getY());
}
@Override
public void paint(Graphics g) {
g.drawImage(background, bg1.getBgX(), bg1.getBgY(), this);
g.drawImage(currentSprite, builder.getCenterX() - 61, builder.getCenterY() - 63, this);
}
My builder class doesn’t move any coordinates on its update() method.
How could I fix this? If more information is needed just say the word!