I have code like:
class Entity;
class EntityTool extends Entity;
class EntitySprite extends Entity;
class EntityToolSpoon extends EntityTool;
class EntityToolBow extends EntityTool;
class EntitySpritePlayer extends EntitySprite;
class EntitySpriteMonster extends EntitySprite;
Now I have a method called move() in Entity, and some others overwrite it.
Now, if the argument is an EntitySpriteMonster, why does this not work:
public void foonction(Object wut)
{
if (wut instanceof Entity) ((Entity)wut).move(x,y);
}
The move function (though this is redundant and unneeded. it fails at if rawpeek instanceof...):
@Override
public void move(double x, double y) {
super.move(x, y);
for (int i=0; i<8; i++) {
Object rawpeek = palette.get(i);
System.out.println(rawpeek);
if (rawpeek == null) continue;
if (rawpeek instanceof Entity)
((Entity) rawpeek).move(x, y);
}
}
EDIT: It just aborts. (operator instanceof returns false)
Eclipse shows me in debug, that wut is definetly an EntitySpriteMonster.
Problem solved!
The above use of instanceof was correct.
The problem instead was:
((Inventory)palette).get(index)returned anInventorySlot, and not aEntity. InventorySlot contains Entites, so I was missing to peek the first item from the container.My correct code should be
Object rawpeek = paltte.get(i).peek();Thanks everyone for trying. And sorry for this obvious problem.