This site requires JavaScript, please enable it in your browser!
Greenfoot back
Chernabog42
Chernabog42 wrote ...

2014/7/8

Can't figure out how to get the x,y of another actor

1
2
danpost danpost

2014/7/9

#
NikZ wrote...
But anyway, yours is shorter, so if people can refer to your code if they choose.
Easier does not necessarily mean shorter (although, that may usually be the case).
thiaya49 thiaya49

2014/7/9

#
tanto per cominciare dove si inizia per un pviluppatore,cioè per intrate in materia.
NikZ NikZ

2014/7/9

#
I'm just saying that I didn't come here to have an argument...
danpost danpost

2014/7/9

#
Since you only need to get the coordinates of the Blank object (fields that are accessible from it as an Actor object; which is what 'getOneIntersectingObject' returns), you could just use the following:
public void check()
{
    if (isTouching(Blank.class))
    {
        Actor blank = getOneIntersectingObject(Blank.class);
        int x = getX();
        int y = getY();
        setLocation(blank.getX(), blank.getY());
        blank.setLocation(x, y);
    }
}
This code would probably be the shortest -- it does not require any additional code in the world constructor; it does not require any class or instance fields in the Tile class; and it is quite to the point. However, this requires unnecessary searches for the Blank object -- one in 'isTouching' and one in 'getOneIntersectingObject'. The 'isTouching' one will be used 8 times per act cycle (once per Tile object) and the 'getOneIntersectingObject' once for each time 'isTouching' returns 'true'. When 'isTouching' returns 'false', it will have iterated through all the objects in the world, determined if any was of the class given and checked the bounds of the images of any found to see if it intersects the bounds of the image of 'this' Tile object. It might be trivial within the context of a slider puzzle; but, in larger projects, where multiple occurrences of time-consuming code is used, this could contribute to lag. Using the 'static' field to hold a reference to the Blank object eliminates the need to find the object. Both objects (the Tile object and the Blank object) are readily available to the 'intersects' method when called and there is no need for 'getOneIntersectingObject' to be used at all.
NikZ wrote...
I'm just saying that I didn't come here to have an argument...
And, I am not looking for one. Cheers.
Chernabog42 Chernabog42

2014/7/9

#
Thank you both for your answers. For now, I'm going to use that last solution for my project, but all of your answers really helped me look at what was going on.
You need to login to post a reply.
1
2