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/11

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public boolean collide(int x, int y, Object object){
       System.out.println(object);
       if (object instanceof Actor){
           if (World.getOneActorAt(x,y,(Actor)object)!=null){
               return true;
           }
       }
        
       if (object instanceof TileType){
           TileType type = World.getTheTileTypeAt(x,y,(TileType) object);
           if (type!=null){
               return true;
           }
       }
        
       return false;
   }
I have Object object be Enemy.class which is a subclass of Matter which is a subclass of Actor, however "object instanceof Actor" is always false. Could someone please help?
danpost danpost

2014/11/11

#
Are you saying it returns a false value when the terminal actually shows that 'object' is of the Enemy class? In what class and method does this method get called from? Please show code. Interesting: this method can be written without any 'if' statements! (lines 3 though 16 can be combined into one statement)
davmac davmac

2014/11/11

#
1
if (World.getOneActorAt(x,y,(Actor)object)!=null){
The World class doesn't have a 'getOneActorAt' method. If this compiles you either have your own World class, or a variable called World; in either case, that's a bad idea.
Entity1037 Entity1037

2014/11/12

#
I should of mentioned: this is eclipse, with my own personally created World and Actor classes. Also, the problem is that this:
1
if (object instanceof Actor){
is always false, and never activates the first if statement. I assign Object object as Enemy.class.
danpost danpost

2014/11/12

#
The 'instanceof' keyword is part of the java language and should work as intended. As you have not responded to my first post, there is little more I can say.
davmac davmac

2014/11/12

#
the problem is that this: ... is always false, ...
How do you know that? And why are you certain that the object really is an instance of Actor? try:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public boolean collide(int x, int y, Object object){ 
    System.out.println("object class = " + object.getClass().getName()); 
    if (object instanceof Actor){ 
        System.out.println("(object instanceof Actor) is true");
        if (World.getOneActorAt(x,y,(Actor)object)!=null){ 
            System.out.println("(World.getOneActorAt(x,y,(Actor)object)!=null) is true");
            return true
        
        System.out.println("(World.getOneActorAt(x,y,(Actor)object)!=null) is false");
    
       
    if (object instanceof TileType){ 
        TileType type = World.getTheTileTypeAt(x,y,(TileType) object); 
        if (type!=null){ 
            return true
        
    
       
    return false
Entity1037 Entity1037

2014/11/14

#
I tried that code, but the only thing that printed was the first println, which printed this every cycle: object class = java.lang.Class object class = java.lang.Class object class = java.lang.Class object class = java.lang.Class object class = java.lang.Class object class = java.lang.Class object class = java.lang.Class object class = data.TileType It seems that it takes Enemy.class as just java.lang.Class. I swear that Enemy.class is whats being put into the thing.
danpost danpost

2014/11/14

#
Try 'obj' instead of 'object'.
Entity1037 Entity1037

2014/11/14

#
Didn't work...
danpost danpost

2014/11/14

#
If you are putting 'Enemy.class' in, then it is correct that 'Enemy.class' is not an 'Actor' object -- it is a 'Class' object. You need to put the Enemy object in for it to return a true value.
danpost danpost

2014/11/14

#
Question: what do you have as the declaration line for the 'getOneActorAt' method?
Entity1037 Entity1037

2014/11/14

#
OH! Thank you!!
danpost danpost

2014/11/14

#
danpost wrote...
Question: what do you have as the declaration line for the 'getOneActorAt' method?
Did you see this post? It really does not make any sense to pass an Actor object to a method that is to find and return an Actor object. It probably is supposed to receive a Class there on line 5; so maybe your line 3 should have 'object instanceof Class' -- or you should check for 'TileType' first and use 'else'.
Entity1037 Entity1037

2014/11/14

#
It says cannot apply getOneActorAt(int, int, Object) to getOneActorAt(int, int, Class) when I do the else, or the instanceof Class, so I just left getOneActorAt as (int,int,Object) and it worked. Thanks for the help!
danpost danpost

2014/11/14

#
So, what did you actually end up with for the code above?
There are more replies on the next page.
1
2