0

So I made my code for my FishingPole class, and I made an instance of Player.

Player player = new Player();
ImageLoader loader = new ImageLoader();
BufferedImage imageFile = loader.loadImage("/Pictures/fishingLine.png");
BufferedImage image = imageFile;

public double x, y;
public static final double FIXED_Y = 251;

Game game;

public FishingPole(Game game){
    this.game = game;
}

public void tick(){
    x = player.getX();
}
public void render(Graphics g){
    g.drawImage(image, (int)x, (int)y, game);
}

public void moveDown(){

}
public void reelIn(){

}

The tick method gets in my game loop every time. However, the fishing line image doesn't move with my player. I tried to do this to see if my getX() method even worked

public void tick(){
    x = player.getX();
    System.out.println(player.getX());
}

That time, when I moved my player, it kept on saying the default position of my player. I also tested this method inside of other classes and it worked fine. This is the code for my getX() method.

public double getX() {
    return this.x;
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
Lt Lobster
  • 41
  • 7
  • Sounds like maybe threading issues, or maybe the EDT not being allowed to update. We'll need more code. Try to make a complete but minimal example that demonstrates the problem. – markspace Jun 10 '15 at 15:09
  • You need to change the value of `x` each time you move. – Uma Kanth Jun 10 '15 at 15:10

1 Answers1

1

It would seem that you create a new instance of Player() at the top of your class. Try passing in a Player object in your constructor (as you do with Game), and get the X from that.

Your code would look like this:

Player player = null;

[snip]

public FishingPole(Game game, Player player){
    this.game = game;
    this.player = player;
}
steenbergh
  • 1,642
  • 3
  • 22
  • 40