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

2014/11/11

Having problems with instanceof.

1
2
Entity1037 Entity1037

2014/11/14

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public boolean collide(int x, int y, Object obj){
         
        if (obj instanceof TileType){
            TileType type = World.getTheTileTypeAt(x,y,(TileType) obj);
            if (type!=null){
                return true;
            }
        }else{
            if (World.getOneActorAt(x,y,obj)!=null){
                return true;
            }
        }
         
        return false;
}
Entity1037 Entity1037

2014/11/14

#
danpost danpost

2014/11/14

#
That is exactly what I meant when I said:
danpost wrote...
-- or you should check for 'TileType' first and use 'else'.
The method can be written quite oddly like this:
1
2
3
public boolean collide(int x, int y, Object obj) {
    return (obj instanceof TileType && World.getTheTileTypeAt(x, y, (TileType)obj) != null) || (!(obj instanceof TileType) && World.getOneActorAt(x, y, obj) != null);
}
Entity1037 Entity1037

2014/11/14

#
Oh yeah, it can! Cool!! Thanks!!!
You need to login to post a reply.
1
2