How would I retrieve the coordinates of my actor subclass in order to place a new actor at the location?

365 Views Asked by At
public void move()
{    
    //if (this.getWorld().getObjects(Marker.class).isEmpty())
    Dog bill = getOneIntersectingObject(Dog.class);
    Marker bone = getOneIntersectingObject(Dog.class);
    if (bone == null); 
    {
        Marker bone= new Marker();
        getWorld().addObject(marker.getX(), marker.getY());
    }
    super.move(1);
}

I'm trying to reference the location of my current actor subclass (dog) in order to place a marker(bone) of another subclass at the coordinates it is located at.

1

There are 1 best solutions below

0
the busybee On

You can retrieve the coordinates of the actor by calling getX() and getY(), as you do it for other actors.

If you like to call methods for the "current" actor, you might like to use this, in example this.getX(). But it is only necessary, if you need to resolve some ambiguity. Commonly you can just call the method.

public void move()
{    
    Dog bill = getOneIntersectingObject(Dog.class);
    Marker bone = getOneIntersectingObject(Marker.class);
    if (bone == null)
    {
        Marker marker = new Marker();
        getWorld().addObject(marker, getX(), getY());
    }
    super.move(1);
}

Changes necessary as obvious from the shown excerpt:

  • The parameter of the second getOneIntersectingObject() is Marker.class, because you seem to want this.
  • No semicolon after the parenthesis of the if. If you don't remove it: This semicolon is an empty statement, so the if will have no effect. The block between the braces following it will always be executed.
  • Rename the second bone as marker to show your intention. (It would "shadow" the "outer" bone, additionally, but this is no problem here.)
  • Call addObject() with the right parameters: the object to place (the new marker), and its coordinates (retrieved by calling getX() and getY()).