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

2017/4/28

test for an object's location

rn42v1r rn42v1r

2017/4/28

#
Hey there, im looking for a method that tests if theres an object at certain coordinates or not. I tried to use the method "getObjectsAt(int x, int y, class)" but I couldnt get it to work the way I wanted it to. I want it to return "true" if there is and return "false" if there isnt. Im aware why it doesnt return "true" or "false" but I cant find a way to make it work the way I want to.
Super_Hippo Super_Hippo

2017/4/28

#
Call the 'isEmpty' method on the list returned by the method to check if the list contains any elements.
rn42v1r rn42v1r

2017/4/28

#
thanks a lot, works great... could you explain to me why there has to be "List" before it?
List klatschen = getObjectsAt(0, 8, Klatsche.class);
if(klatschen.isEmpty()){
   addObject(new Klatsche(), 0, 9);
}
Super_Hippo Super_Hippo

2017/4/28

#
'getObjectsAt' returns a List, so if you want to save it as a (temporary) variable, you need a List variable. However, you can also call the 'isEmpty' method directly without saving it in "klatschen" first:
if (getObjectsAt(0, 8, Klatsche.class).isEmpty())
{
    //...
}
rn42v1r rn42v1r

2017/4/28

#
Ah ok. I noticed that, just was curious about the "list" thing. Thanks again
You need to login to post a reply.