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

2019/5/30

Regarding an error we are receiving

Joyfu1 Joyfu1

2019/5/30

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Ship here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ship extends Actor
{
    // True or false statement for touching other actors
    boolean touchingAlien = false;
    boolean touchingUFO = false;
    boolean touchingUFOLaser = false; 

    private ScoreBoard score;

    public Ship(ScoreBoard score)
    {
        this.score = score;
    }

    /**
     * Act - do whatever the Ship wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        movement();
        Boundry(); 
        hitAlien();
        hitUFO();
    }  

    public void movement() {
        // User input for navigating ship
        if (Greenfoot.isKeyDown("up")) {
            setLocation(getX(), getY() - 5);
        }

        if (Greenfoot.isKeyDown("down")) {
            setLocation(getX(), getY() + 5);
        }

        if (Greenfoot.isKeyDown("right")) {
            setLocation(getX() + 5, getY());
        }

        if (Greenfoot.isKeyDown("left")) {
            setLocation(getX() - 5, getY());
        }
        // User input for shooting lasers (works when key is lifted)
        if ("space".equals(Greenfoot.getKey())) {
            shoot();
        }
    }

    public void shoot() {
        // Add laser object to world
        getWorld().addObject(new Laser(score), getX(), getY()); 
    }

    public void hitAlien() {

        // Define the actor as the ship touching an alien
        Actor alien = getOneIntersectingObject(Alien.class);

        if (alien != null ) {
            // Call MyWorld and HealthBar into ship class
            World myWorld = getWorld();
            MyWorld myworld = (MyWorld)myWorld;

            HealthBar healthbar = myworld.getHealthBar();
            if (touchingAlien == false) {
                healthbar.loseHealth();
                touchingAlien = true;  
                if (healthbar.health <= 0) {
                    // Game over since health has hit 0
                    myWorld.removeObject(this);
                    Greenfoot.setWorld(new GameOver());
                }
            }

            else {
                // Continue game 
                touchingAlien = false;
            }
        }

    }
    public void hitUFO() {
        // Define the actor as the ship touching an alien
        Actor UFO = getOneIntersectingObject(UFO.class);
        Actor UFOLaser = getOneIntersectingObject(UFOLaser.class);
        if (UFO !=null || UFOLaser !=null ) {
            // Call MyWorld and HealthBar into ship class

            World finalWorld = getWorld(); 
            FinalWorld finalworld = (FinalWorld)finalWorld; 

            HealthBar healthbar = finalworld.getHealthBar();
            if (touchingUFO == false || touchingUFOLaser == false) {
                healthbar.loseHealth();
                touchingUFO = true;
                touchingUFOLaser = true;
                if (healthbar.health <= 0) {
                    // Game over since health has hit 0
                    finalWorld.removeObject(this);
                    Greenfoot.setWorld(new GameOver());
                }
            }

            else {
                // Continue game 
                touchingUFO = false;
                touchingUFOLaser = false; 
            }

        }
    }

    public void Boundry() {
        if(getX() > 980) {
            setLocation(980, getY());
        }
        if(getX() < 0) {
            setLocation(0, getY()); 
        }
        if(getY() > 575) {
            setLocation(getX(), 575);
        }
        if(getY() < 0) {
            setLocation(getX(), 0); 
        }

    }
}
Basically, we are getting an error every time an Alien hits our ship in MyWorld. The game over screen works perfectly fine in FinalWorld. Thank you!
danpost danpost

2019/5/30

#
The hitUFO method requires the ship be in a world to execute. However, the hitAlien method, which is called first, has the potential to remove the ship from the world. You must ensure the ship is in the world before calling hitUFO. Change line 32 to the following:
if (getWorld() != null) hitUFO();
Joyfu1 Joyfu1

2019/5/30

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