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

2022/2/7

DON'T do method if actor does not exist

NUKES NUKES

2022/2/7

#
I have a problem here. I made a code so, when the player gets hit by a ball, it will show a game over screen. I have multiple balls which show up when it gets shot by a bullet. The problem is, that when I shoot a bullet, it will get removed from the world, but then, since I have a game over method, it will still look for the ball and give an error saying, Actor does not exist in world. How can I make it so, the method only gets called, when the Ball exists? I tried something like this but it does not seem to work.
public void hitball()
    {
        if ( ! getWorld().getObjects(ball.class).isEmpty()) {
            Actor ball = getOneIntersectingObject(ball.class);
            if (ball != null)
            {
                World myWorld = getWorld();
                GameOver gameover = new GameOver();
                myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
                myWorld.removeObject(this);
            }
        }   else {
            return;
        }
    }
    public void hitball2()
    {
        if ( ! getWorld().getObjects(ball2.class).isEmpty()) {
            Actor ball2 = getOneIntersectingObject(ball2.class);
            if (ball2 != null)
            {
                World myWorld = getWorld();
                GameOver gameover = new GameOver();
                myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
                myWorld.removeObject(this);
            }
        }   else {
            return;
        }
    }
    public void hitball4()
    {
        if ( ! getWorld().getObjects(ball4.class).isEmpty()) {
            Actor ball4 = getOneIntersectingObject(ball4.class);
            if (ball4 != null)
            {
                World myWorld = getWorld();
                GameOver gameover = new GameOver();
                myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
                myWorld.removeObject(this);
            }
        }   else {
            return;
        }
    }   
    public void hitball8()
    {
        if ( ! getWorld().getObjects(ball8.class).isEmpty()) {
            Actor ball8 = getOneIntersectingObject(ball8.class);
            if (ball8 != null)
            {
                World myWorld = getWorld();
                GameOver gameover = new GameOver();
                myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
                myWorld.removeObject(this);
            }
        }   else {
            return;
        }
    }
If anyone knows, how I can fix this easily, please help out, thanks!!
danpost danpost

2022/2/7

#
For each subsequent call to a "hitball#" method called in act, add the following condition:
if (getWorld() != null)
So, like this:
public void act()
{
    ...
    hitball();
    if (getWorld() != null) hitball2();
    if (getWorld() != null) hitball3();
    if (getWorld() != null) hitball4();
    // also after all calls to hitball, add this one line
    if (getWorld() == null) return;
    ...
}
danpost danpost

2022/2/7

#
Or, just use the last line repeatedly after each call:
hitball();
if (getWorld() == null) return;
hitball2();
if (getWorld() == null) return;
hitball3();
if (getWorld() == null) return;
hitball4();
if (getWorld() == null) return;
NUKES NUKES

2022/2/7

#
Thank you for the reply, I will try this next, but I have a different question. Is it possible that you and I maybe sit down and look at this together in a meeting or something? I will gladly pay for your time aswell, im pretty deep in some stuff and need to finish this as soon as possible and I would like to just talk to you personally through an Email or something. If that is possible, please let me know.
NUKES NUKES

2022/2/7

#
I tried both ways and it does not seem to work. I still get the same error. Below is the whole code block of the shooter class (Xatar).
public class xatar extends Actor
{
    /**
     * Act - do whatever the xatar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void shoot()
    {
     getWorld().addObject(new bullet(), getX(), getY()); //this will add a new Bullet object (change the name to whatever you want to call the bullet/projectile class) to the world at the spaceships location.
    }
    private boolean spaceKeyDown;
    public void act()
    {
        if (Greenfoot.isKeyDown("a")== true)
        {
            this.move(-10);
            
            
        }
        else if (Greenfoot.isKeyDown("d")== true)
        {
            this.move(10);
            
            
        }
        {
            if (spaceKeyDown != Greenfoot.isKeyDown("space")) // detect change in state of space key
            {
                spaceKeyDown = ! spaceKeyDown; // tracking state of space key
                if (spaceKeyDown) shoot();
            }
        }
        hitball();
        if (getWorld() == null) return;
        hitball2(); 
        if (getWorld() == null) return;
        hitball4(); 
        if (getWorld() == null) return;
        hitball8(); 
        if (getWorld() == null) return;
    }
    public void hitball()
    {
        if ( ! getWorld().getObjects(ball.class).isEmpty()) {
            Actor ball = getOneIntersectingObject(ball.class);
            if (ball != null)
            {
                World myWorld = getWorld();
                GameOver gameover = new GameOver();
                myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
                myWorld.removeObject(this);
            }
        }   else {
            return;
        }
    }
    public void hitball2()
    {
        if ( ! getWorld().getObjects(ball2.class).isEmpty()) {
            Actor ball2 = getOneIntersectingObject(ball2.class);
            if (ball2 != null)
            {
                World myWorld = getWorld();
                GameOver gameover = new GameOver();
                myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
                myWorld.removeObject(this);
            }
        }   else {
            return;
        }
    }
    public void hitball4()
    {
        if ( ! getWorld().getObjects(ball4.class).isEmpty()) {
            Actor ball4 = getOneIntersectingObject(ball4.class);
            if (ball4 != null)
            {
                World myWorld = getWorld();
                GameOver gameover = new GameOver();
                myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
                myWorld.removeObject(this);
            }
        }   else {
            return;
        }
    }
    public void hitball8()
    {
        if ( ! getWorld().getObjects(ball8.class).isEmpty()) {
            Actor ball8 = getOneIntersectingObject(ball8.class);
            if (ball8 != null)
            {
                World myWorld = getWorld();
                GameOver gameover = new GameOver();
                myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
                myWorld.removeObject(this);
            }
        }   else {
            return;
        }
    }
}
danpost danpost

2022/2/7

#
Here is a simplified version of your class:
public class xatar extends Actor
{
    private boolean spaceKeyDown;
    
    public void act()
    {
        if (Greenfoot.isKeyDown("a")) move(-10);
        if (Greenfoot.isKeyDown("d")) move(10);
        if (spaceKeyDown != Greenfoot.isKeyDown("space"))
        {
            spaceKeyDown = ! spaceKeyDown; // tracking state of space key
            if (spaceKeyDown) getWorld().addObject(new bullet(), getX(), getY());
        }
        if (isTouching(ball.class) || isTouching(ball2.class) ||
            isTouching(ball4.class) || isTouching(ball8.class)) 
        {
            getWorld().addObject(new GameOver(), getWorld().getWidth()/2, getWorld().getHeight()/2);
            getWorld().removeObject(this);
        }
    }
}
You need to login to post a reply.