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

2018/1/7

checking if an Object is removed

L2plex L2plex

2018/1/7

#
Hello For my Labyrinth I created an actor called Flashlight. If the player cross this Item it should disappear. This is the code of my Player actor;
        
        Actor localFlashlight = getOneIntersectingObject(Flashlight.class);
                
        if(intersects(localFlashlight)){ 
            // do something
            setLocation(400,100);   
        }
        getWorld().removeObject(localFlashlight); //gefressenes Item entfernen 
    
What I want is that the item gets removed (works fine) and then something happens (the Player can see further = not done yet) When I run this application I get a "NullPointerException" error. The problem is the if statement.... How can I check if the Player crosses a flashlight? (there will be multiple flashlights)
danpost danpost

2018/1/7

#
If not intersecting a Flashlight object 'localFlashlight' will be 'null' and you need an actual actor for the 'intersects' method. Anyway, why would you ask if it intersects an object that you already found to be intersecting (or not). Just ask if 'localFlashlight' is 'null'.
L2plex L2plex

2018/1/7

#
Thanks danpost. I replaced the "intersects(localFlashlight)" with "localFlashlight != null" and it works fine. ; )
You need to login to post a reply.