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

2016/5/18

Help with understanding getOneIntersectingObject

Bat8 Bat8

2016/5/18

#
Hi, I'm new to programming and I'm creating a little game with Greenfoot. In that game, the "Player" class is supposed to check if the player is touched by an enemie. I created an "Enemie" class with several subclasses to code some aspects differently for some enemies. However, when the player is touched by any enemie, the game should restart. I got everything working as it should be, but I just copied the use of getOneIntersectingObject from another scenario and changed everything I needed to change:
1
2
3
4
if (getOneIntersectingObject(Enemie.class) != null)
        {
           my code
        }
This code is working fine, but I want to understand it aswell. The method getOneIntersectingObject checks if an enemie and the player are intersecting, but what does the "null" stand for and what type of information is that? Thank you for answering!
SPower SPower

2016/5/18

#
The getOneIntersectingObject returns one of the objects that is currently intersecting the actor calling the function. If many enemies are touching your actor, then, it would only return one of those. If, however, there are no objects currently intersecting it, then it cannot return an object, so it returns null. Null is the type for 'no object', you could say. So, the if-statment is to check if the function does not return null, i.e. if it does return an object.
Bat8 Bat8

2016/5/18

#
SPower wrote...
The getOneIntersectingObject returns one of the objects that is currently intersecting the actor calling the function. If many enemies are touching your actor, then, it would only return one of those. If, however, there are no objects currently intersecting it, then it cannot return an object, so it returns null. Null is the type for 'no object', you could say. So, the if-statment is to check if the function does not return null, i.e. if it does return an object.
Thank you! I was looking for an explanation on the internet. All of them were very long and complicated and in the end I still didn't understand. You explain it in five lines and I completely understand it. :D
You need to login to post a reply.