0

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
  );
 }
}
  • If you haven't already, add some log statements to your methods to (1) confirm they're being called and (2) check values. – DavidS Mar 02 '16 at 23:12
  • Static variables belong to the class instead of to the individual instances, so it doesn't change in every instance - it only exists in one place. It may give the appearance of changing in every instance, but that is because there is only one variable. – Matthew Mar 02 '16 at 23:12
  • 1
    You have an error in Game.init. The line should read `move = new MoveChara();` - you are missing the parentheses. – Matthew Mar 02 '16 at 23:13
  • Regarding static variables, @Matthew, I think he knows this, although his wording is ambiguous. Either way, he should still see the character move, just if he tried to make multiple characters they will all have the same coordinates. – DavidS Mar 02 '16 at 23:14
  • So where is the question or problem statement? – Drew Mar 02 '16 at 23:16
  • Where is the definition of the render method in the Game class? I strongly suspect that you are calling init on MoveChara too many times. Each time you call that you reset the variables to the defaults. – Matthew Mar 02 '16 at 23:19
  • Thanks everybody, Matthew was right, I was calling move.init over and over again – Charlie Landrigan Mar 02 '16 at 23:32

2 Answers2

5

A static variable belongs to the class not to an instance of the class. To access a static variable from outside of its class you can do it like this:

ClassName.variableName = newValue;

Inside the paintComponent method there is a call to the init method in the parent class, this is initializing x and y everytime it is called. If you move the initialization to the static variables declarations this dhould work. Let me know if it's this you are looking for.

Abhishek Thapliyal
  • 3,497
  • 6
  • 30
  • 69
goncalopinto
  • 433
  • 2
  • 8
  • This doesn't answer the question. He says that "I am trying to display a character moving across the screen but the x and y variables stay the same." Even with static variables, he should still see his character move if he codes things properly. – DavidS Mar 02 '16 at 23:24
  • 2
    I edited my answer with complementary information that should resolve the issue. – goncalopinto Mar 02 '16 at 23:31
0

Static variables are shared across instances of the class. When edited, they change for all instances.

Top Sekret
  • 748
  • 5
  • 21
  • 2
    This doesn't answer the question. He says that "I am trying to display a character moving across the screen but the x and y variables stay the same." Even with static variables, he should still see his character move if he codes things properly. – DavidS Mar 02 '16 at 23:19
  • 1
    This really doesn't answer the question. In fact Matthew's comment above is even better than your answer, I.e. "Static variables belong to the... but that is because there is only one variable." – Tacocat Mar 02 '16 at 23:22