0

Originally, this started as a nonstatic method being called from a static context error. I've since realized that I cannot reference the class itself, but a specific instance of that class. However, after giving my initialized object a specific name, I find that java will not acknowledge this specific instance, but instead refer to is as an unknown variable. When attempting to compile the code, I receive the error " cannot find symbol - variable player". I believe the code isn't recognizing the particular instance of the class as an already declared variable.

Here is the class being initialized

Link player = new Link(); 
addObject(player, 300, 176);

and here is the method trying to reference this specific instance's method:

int linkX = player.getX();

The second bit of code is in a method belonging to a class by the name of ChuChu. ChuChu and Link are subclasses of the same superclass. Any help would be greatly appreciated!

World class:

  public Link player;

/**
 * Constructor for objects of class ThroneRoom.
 * 
 */
public ThroneRoom()
{    
    // Create a new world with 600x562 cells with a cell size of 1x1 pixels.
    super(600, 562, 1);
    this.player = new Link(); 
    prepare();
}

/**
 * Prepare the world for the start of the program. That is: create the initial
 * objects and add them to the world.
 */
public void prepare() //R1
{
    addObject(player, 300, 176);
    addObject(new ChuChu(), 45, 267);
    addObject (new ChuChu(), 558, 267);
    addObject ( new ChuChu(), 45, 373);

}

Method for ChuChu in full

/**
 * Crawl Toward - Makes ChuChus crawl toward Link
*/
public void crawlToward ()
{
    int random = (int) Math.random() * 5;
    int linkX = player.getX();
    int linkY = player.getY();
    if(getX() >linkX)
    {
        setLocation(getX() - random, getY());
    }
    else
    {
        setLocation(getX()+random, getY());
    }

    if(getX() > linkY )
    {
        setLocation(getX(), getY()-random);
    }
    else
    {
        setLocation(getX(), getY()+random);
    }

}

I am using an IDE called Greenfoot. My problem seems to be that I am given two classes, World and Actor, and they cannot inherit from each other. From the World class, I instance objects into the visual world, and in Actor I create the objects and their methods.

user3055592
  • 1
  • 1
  • 2
  • That looks okay (as long as `getX` is a public method), we need some more context, I guess. What kind of error are you getting and where? – Thilo Dec 01 '13 at 23:45
  • 2
    It would be really helpful if you didn't capitalize your variable names. Player.getX() looks like a static call to a class called Player. – tjg184 Dec 01 '13 at 23:45
  • Why would "Greenfoot software" be more context? There's too much irrelevant text in your post. Please specify the actual problem. If `addObject()` is supposed to mutate your `Player` object, then you are doing OO wrong. – Jeroen Vannevel Dec 01 '13 at 23:45
  • 1
    Not nearly enough code. At least show the class definition, constructor and the method calling `getX`. – Chris Hayes Dec 01 '13 at 23:46

1 Answers1

0

Based on your clarifying comment this might help, Java is block scoped so something like this wouldn't work:

if(true) {
  Link player = new Link(); 
}
//player is outside of the block it was declared in
int linkX = player.getX();

In your case y need to declare player outside of private void prepare() (at the class level would make the most sense in this case). you are free to instantiate it inside a block (or method in your case). Here is a quick attempt at doing this with your class:

public class World
{
  Link player;

  public ThroneRoom()
  {    
    // Create a new world with 600x562 cells with a cell size of 1x1 pixels.
    super(600, 562, 1); 

    prepare();
  }

  /**
   * Prepare the world for the start of the program. That is: create the initial
   * objects and add them to the world.
   */
  private void prepare()
  {
    this.player = new Link();
    addObject(player, 300, 176);
    addObject(new ChuChu(), 45, 267);
    addObject (new ChuChu(), 558, 267);
    addObject ( new ChuChu(), 45, 373);

  }
  public void crawlToward ()
  {
    int random = (int) Math.random() * 5;
    int linkX = this.player.getX();
    int linkY = this.player.getY();
    if(getX() >linkX)
    {
      setLocation(getX() - random, getY());
    }
    else
    {
      setLocation(getX()+random, getY());
    }

    if(getX() > linkY )
    {
      setLocation(getX(), getY()-random);
    }
    else
    {
      setLocation(getX(), getY()+random);
    }
  }
}
Jason Sperske
  • 29,816
  • 8
  • 73
  • 124
  • That didn't work, but I believe this is on the right track. I'm instancing "player" into my World class, and I'm trying to get the ChuChu class to recognize the "player" instance as already existing, and not an undeclared variable. I'll do some research into block scoping. This is my first semester of programming, and I'm a bit unfamiliar with the term. – user3055592 Dec 02 '13 at 00:00
  • You also had a bug related to the line `int linkY = Link.getY();` – Jason Sperske Dec 02 '13 at 00:06
  • My problem seems to be that I am given two classes, World and Actor, and they cannot inherit from each other. From the World class, I instance objects into the visual world, and in Actor I create the objects and their methods. This is preventing me from using the code provided. – user3055592 Dec 02 '13 at 00:33