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

2020/3/15

how do I check if an actor still exists

bangtan bangtan

2020/3/15

#
I want to check if there's still an existing actor before running another method
danpost danpost

2020/3/15

#
bangtan wrote...
I want to check if there's still an existing actor before running another method
if (someActor.getWorld() != null)
where someActor references a specific actor instance. Or
if ( ! getWorld().getObjects(SomeActor.class).isEmpty())
where SomeActor is the type of actor (for any instances of that type)
bangtan bangtan

2020/3/15

#
thank you so much!!!!1
bangtan bangtan

2020/3/15

#
danpost wrote...
bangtan wrote...
I want to check if there's still an existing actor before running another method
if (someActor.getWorld() != null)
where someActor references a specific actor instance. Or
if ( ! getWorld().getObjects(SomeActor.class).isEmpty())
where SomeActor is the type of actor (for any instances of that type)
is there a way in which if the actor was removed, it will not do a method
danpost danpost

2020/3/15

#
bangtan wrote...
is there a way in which if the actor was removed, it will not do a method
Place one of those condition on calling the method.
bangtan bangtan

2020/3/15

#
like this?
public void exist(){
        if ( ! getWorld().getObjects(runner.class).isEmpty()){
            getWall();
        }
    }
danpost danpost

2020/3/15

#
bangtan wrote...
like this? << Code Omitted >>
Need more context. Show entire class codes.
bangtan bangtan

2020/3/15

#
boolean needs to work if there's the runner, but if the runner gets killed, getWall shouldn't run because it will be an error
public class runner extends Actor
{  
    
    
    int health = 5;
 
    public void exist(){
        if ( ! getWorld().getObjects(runner.class).isEmpty()){
            getWall();
            
        }
    }
    public void act()
    {
        exist();
        health();
        kill();
        
        if(getWall() == false) 
        {
             move(5); 
        } else { 
            int turn = Greenfoot.getRandomNumber(3);
         
            if(turn == 0) 
            {
                turn(90);
            } else if (turn == 1){
                turn(-90);
            } else if (turn == 2){
                turn(180);
            } else if (turn == 3){
                turn(-180);
            }

        }

    private boolean getWall(){
        {
            GreenfootImage myImage = getImage();
            int distance = myImage.getWidth()/2;
            int xOffset = (int)Math.ceil(distanceToFront*Math.cos(Math.toRadians(getRotation())));
            int yOffset = (int)Math.ceil(distanceToFront*Math.sin(Math.toRadians(getRotation())));
            
            Actor wall = getOneObjectAtOffset(xOffset, yOffset, wall.class);
            return(wall != null);
            
        } 
    }

   
    public void health(){
      Actor shot = getOneIntersectingObject(bullet.class);
        if (bullet != null) {
            health = health - 1;
      }
    }
    public void kill(){
        if (health == 0){
            getWorld().removeObject(this);
        }
    }
}
  

danpost danpost

2020/3/15

#
Oh. You want the runner to check if it, itself, is in the world. Alright. Remove lines 7 thru 12 and also line 15. Then, put the following at line 18:
if (getWorld() == null) return;
That will immediately exit the method (not letting any further code in the method to execute) if the actor was removed from the world.
bangtan bangtan

2020/3/16

#
thank you!!
You need to login to post a reply.