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

2013/10/20

isTouching vs getIntersectingObjects

ddvink ddvink

2013/10/20

#
Here's some code
 public void checkForItems(){
        if(getIntersectingObjects(Coin.class)){
            System.out.println("Ik zie een coin!!!");
        }
        
//         if(isTouching(Coin.class)){
//             System.out.println("Ik zie een coin!!!");
//         }
    }
Now the isTouching (which is commented now) is working this way, the removeTouching or getIntersectingObjects isn't working.... What am I doing wrong? Yours, Dennis
danpost danpost

2013/10/20

#
I do not know how you are trying to use the 'removeTouching' method; but, I know that
if (getIntersectingObjects(Coin.class))
will not work because it returns a list (not a true/false value). You need to add '.isEmpty()' or '.size() != 0' or anything to make it a true/false expression.
Gevater_Tod4711 Gevater_Tod4711

2013/10/20

#
The method getIntersectingObjects returns a list (java.util.List) of objects. You can't check the list as a boolean. isTouching returns a boolean which is either true or false. In the if clause you can only use this values. You can't just check the list. If you change the if clause to if (!getIntersectingObjects(Coin.class).isEmpty()) it will work like the isTouching method. isEmpty checks whether the list contains any objects. So if it's not empty you are touching a coin.
ddvink ddvink

2013/10/20

#
Thanx very much both of you!!!!
You need to login to post a reply.