I am trying to display a character moving across the screen but the x and y variables stay the same. I am under the impression that when I change the value of a static variable it changes in every instance of the class.
here is the class where is meant to move the character
public class MoveChara {
private static int x, y;
private int dx, dy;
public void init() {
x = 30;
y = 50;
dx = 1;
dy = 1;
}
public void move() {
x += dx;
y += dy;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
here is part of the class that calls the move method
public class Game implements Runnable {
private MoveChara move;
private boolean running = false;
public void run() {
init();
while(running) {
tick();
render();
}
stop();
}
private void init() {
move = new MoveChara;
}
private void tick() {
move.move();
}
}
and in the method that draws the character
public class Draw extends JPanel {
public MoveChara move;
public ImageMake imgm;
@Override
public void paintComponent(Graphics g) {
imgm = new ImageMake();
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
move = new MoveChara();
move.init();
g2d.drawImage(
imgm.createImg("Images/SpriteSheet.png"),
move.getX(),
move.getY(),
this
);
}
}