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

2017/5/26

Where is my error

asmith1998 asmith1998

2017/5/26

#
When i shoot at the Missile it comes up with error
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Astroid here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Missile extends Objects {
    Scoreboard Scoreboard;
   boolean touchingMissile;
    public Missile(Scoreboard pointScoreboard) {
        Scoreboard = pointScoreboard;
    }

    public void act() {
        movement();
        makeMissile();
        removeMissile();
    }

    /**
     * The movement and rotation for the missile.
     */
    private void movement() {
        int x = getX() -3;
        int y = getY();
        setLocation(x, y);
    }

    /**
     * If the missile is at the edge of the world or is hit by a bullet shot
     * it will be removed and it will create a new missile. Also randomly
     * creates missile
     */
    public void makeMissile() {
        World world = getWorld();
        Missile newMissile = new Missile(Scoreboard);
        int worldHeight = world.getHeight();
        int worldWidth = world.getWidth();
        if(Greenfoot.getRandomNumber(9999) < 9) {
            world.addObject(newMissile, worldWidth, Greenfoot.getRandomNumber(worldHeight));
        }
    }

    public void removeMissile() {
        World world = getWorld();
        Missile newMissile = new Missile(Scoreboard);
        int worldHeight = world.getHeight();
        int worldWidth = world.getWidth();
        if(atWorldEdge()&& this != null) {
            world.removeObject(this);
            world.addObject(newMissile, worldWidth, Greenfoot.getRandomNumber(worldHeight));
        }
        if(onContact(Bullet.class) && this != null) {
            Scoreboard.add(100);
            world.removeObject(this);
            world.addObject(newMissile, worldWidth, Greenfoot.getRandomNumber(worldHeight));
        }
    }
}
Super_Hippo Super_Hippo

2017/5/26

#
Change the 'if' in line 55 to 'else if'. In the future, please provide the error message, so we don't have to guess what could be wrong. Btw, 'this' will never be 'null'.
You need to login to post a reply.