0

Probably a very basic Java problem but I have two variables in a entity class:

public class Entity {

int posX;
int posY;

public Entity(int posX, int posY){
    this.posX = posX;
    this.posY = posY;
}

public void update(){

}

public void draw(Graphics2D g2d){

}

}

My player and enemy class extend it and then render off of the two variables. Like so:

public void draw(Graphics2D g2d) {
    g2d.drawImage(getPlayerImg(), posX, posY, null);
    if (showBounds == true) {
        g2d.draw(getBounds());
    }
}

I need to access these variables like this (This is in my enemy class):

public static void moveFemale(){
    if(posX <= Player.posX){
        //do AI code
    }
}

posX And Player.posX throw an error saying I need to change the modifier of posX in Entity.java to static. But when I change it to static, my renderer for the enemy class stops working and the enemies no longer show up on screen. How could I go about creating a variable that allowed me to do this:

public static void moveFemale(){
    if(posX <= Player.posX){
        //do AI code
    }
}

And still render my enemies? Sorry for the wall of text and any answers would help a lot!

  • 2
    I'd recommend you to read about the keyword `static` and decide if you really need to have your methods declared as `static`. – Andrew Logvinov Oct 28 '12 at 15:53
  • I'm guessing this is what you're looking for: http://stackoverflow.com/questions/6320190/java-extended-static-class-of-a-non-static-class – Nano Oct 28 '12 at 16:38

2 Answers2

2

The moveFemale method is static so it needs to know which female to move. Either pass in a reference to the palyer or find a may to make the move methods non-static member methods of the player.

Andrew White
  • 52,720
  • 19
  • 113
  • 137
0

I think, you should remove modifier static from method moveFemale

 public void moveFemale(){
    if(posX <= Player.posX){
        //do AI code
    }
  }
yatul
  • 1,103
  • 12
  • 27