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

2017/2/7

getOneIntersectingObject

NatNat NatNat

2017/2/7

#
Hello there! I've got a little problem with getOneIntersectingObject:
 public int getValue(int x, int y)
    {
        Tile tiletemp = new Tile();
        tiletemp.setLocation(x, y);
        tiletemp = getOneIntersectingObject(Tile.class);
        return tiletemp.value;
    }
As you can see I try to get another Object of class Tile which intersects my new Object but the error message is: incompatible types: greenfoot.Actor cannot be converted to Tile what can i do about that? thanks for your help!
Super_Hippo Super_Hippo

2017/2/7

#
Right now the code tries this: - create a new Tile - set that tile to the coordinate which were passed to this method //I am pretty sure that you have to add it to the world first - set tiletemp to a tile which is intersecting the object this getValue method is called on - return the value variable of the intersecting tile As much as I see from your text, you want to get the value of a tile which is intersecting the tiletemp. I am not sure how this could make sense, but then you need to call the 'getOneIntersectingObject' on the tiletemp. However, this will probably not work because these methods are protected, so you need a method in the tile class.
public int getValue(int x, int y)
{
    Tile tiletemp = new Tile();
    getWorld().addObject(tiletemp, x, y);
    Tile tiletemp2 = tiletemp.getIntersectingTile();
    getWorld().removeObject(tiletemp);
    return tiletemp.value;
}
public Tile getIntersectingTile()
{
    return getOneIntersectingObject(Tile.class);
}
danpost danpost

2017/2/7

#
The 'getOneIntersectingObject method returns an object reference of type Actor; but, you are trying to assign it to a variable of type Tile -- hence, the error. Just change line 5 to the following so the compiler knows that the returned reference is for an object of type Tile:
tiletemp = (Tile)getOneIntersectingObject(Tile.class);
(maybe this is not the problem and generics takes care of this particular thing)
You need to login to post a reply.