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
Chernabog42 Chernabog42

2014/7/8

#
I am trying to make a slide puzzle game. I have 8 "Tile" actors and one "Blank" actor on the screen. I want my Tile actor to check if is touching the Blank actor. If it is, I want the Tile to get the x,y coordinate of the Blank actor, and I wnt them to swap places. The code I wrote that I thought would accomplish this is: }
/**
     * Check if tile is next to open space
     */
    public void check()
    {
        if (isTouching(Blank.class))
        {            
            int x1= Blank.getX();
            int y1= Blank.getY();
            int x2= getX;
            int y2= getY;
            setLocation(x1,y1);
            Blank.setLocation(x2,y2);            
        }
but when I compile, I get "non-static method getX() cannot be referenced from a static context" Any nudges as to what I did wrong would be great thanks.
NikZ NikZ

2014/7/8

#
You have getX without "()" after it (also getY) on lines 10 and 11. Also you cannot use "Blank."
NikZ NikZ

2014/7/8

#
As in, the "Blank" you refer to needs to be variable (so it corresponds to the Blank in the isTouching()).
NikZ NikZ

2014/7/8

#
/**
     * Check if tile is next to open space
     */
    public void check()
    {
        if (isTouching(Blank.class))
        {
            //right here you can get the blank, maybe name it "b1"            
            int x1= b1.getX();
            int y1= b1.getY();
            int x2= getX;
            int y2= getY;
            setLocation(x1,y1);
            b1.setLocation(x2,y2);            
        }
NikZ NikZ

2014/7/8

#
There are many ways "get" the Blank. If there is only one, you can use
List blanks= getWorld().getObjects(Blank.class);
NikZ NikZ

2014/7/8

#
Then you can use
Blank b1 = (Blank)blanks.get(0);
NikZ NikZ

2014/7/8

#
This is for multiple blanks:
for (int counter = 0; counter < blanks.size(); counter++) {
                        Blank b1= (Blank)blanks.get(counter);

                        if (intersects(b1)) {
                            int x1= b1.getX();  
            int y1= b1.getY();  
            int x2= getX;  
            int y2= getY;  
            setLocation(x1,y1);  
            b1.setLocation(x2,y2);              
                        }
I believe this might do it...
danpost danpost

2014/7/8

#
An easier way would be to have a 'public static Blank blank' field in your Tile class so that all Tiles will have a reference to it. You would use something like the following in the constructor of your sub-class of World:
Blank blank = new Blank();
Tile.blank = blank;
addObject(blank, 2, 2); // wherever
Then your method above would be:
public void check()
{
    if (intersects(blank))
    {
        int x = getX();
        int y = getY();
        setLocation(blank.getX(), blank.getY());
        blank.setLocation(x, y);
    }
}
NikZ NikZ

2014/7/8

#
danpost wrote...
An easier way would be to have a 'public static Blank blank' field in your Tile class so that all Tiles will have a reference to it. You would use something like the following in the constructor of your sub-class of World:
Blank blank = new Blank();
Tile.blank = blank;
addObject(blank, 2, 2); // wherever
Then your method above would be:
public void check()
{
    if (intersects(blank))
    {
        int x = getX();
        int y = getY();
        setLocation(blank.getX(), blank.getY());
        blank.setLocation(x, y);
    }
}
Wouldn't you need public access for blank?
danpost danpost

2014/7/8

#
NikZ wrote...
Wouldn't you need public access for blank?
What are you referring to? I wrote 'public static Blank blank'.
NikZ NikZ

2014/7/8

#
danpost wrote...
NikZ wrote...
Wouldn't you need public access for blank?
What are you referring to? I wrote 'public static Blank blank'.
Well, I was just posting code people can use if they want to avoid using "public." But anyway, yours is shorter, so if people can refer to your code if they choose.
danpost danpost

2014/7/8

#
NikZ wrote...
danpost wrote...
NikZ wrote...
Wouldn't you need public access for blank?
What are you referring to? I wrote 'public static Blank blank'.
Well, I was just posting code people can use if they want to avoid using "public." But anyway, yours is shorter, so if people can refer to your code if they choose.
They could use 'private static Blank blank' and add the following 'set' method to the Tile class:
public static void setBlank(Blank blnk)
{
    blank = blnk;
}
Then in the world constructor:
Blank blank = new Blank()
Tile.setBlank(blank);
addObject(blank, 2, 2);
NikZ NikZ

2014/7/8

#
Yes, that works, but isn't that an extra method? I'm not saying your code is bad. And, as you said, your code is shorter than mine.
danpost danpost

2014/7/9

#
NikZ wrote...
Yes, that works, but isn't that an extra method? I'm not saying your code is bad. And, as you said, your code is shorter than mine.
Yes. It would be an extra method (the cost of making the static field 'private' -- given as an option). You said my code was shorter, not I.
NikZ NikZ

2014/7/9

#
danpost wrote...
You said my code was shorter, not I.
danpost wrote...
An easier way would be to have a 'public static Blank blank' field in your Tile class so that all Tiles will have a reference to it.
Hmmm... really? :)
There are more replies on the next page.
1
2