getX() works in all my classes except this one

115 views Asked by At

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;
}
1

There are 1 answers

0
steenbergh On

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;
}