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

2014/7/9

Please help - implementing different scoring based on...

1
2
blangley83 blangley83

2014/7/10

#
After applying everything we covered, i ran the game and came to the terminal window with these: java.lang.NullPointerException at Asteroid.breakUp(Asteroid.java:82) at Asteroid.hit(Asteroid.java:67) at Bullet.checkAsteroidHit(Bullet.java:51) at Bullet.act(Bullet.java:39) Also noticing i am getting scores of 16 and 33...random scores that should not be possible to get. That is... i get these scores before that terminal window above appears :) </3 i dont know how you do this dan!! it is heart breaking
danpost danpost

2014/7/10

#
Please re-post your Asteroid class (as it is now). Also, re-produce the error and re-post the error message. (I want the exact code that the exception was thrown with)
blangley83 blangley83

2014/7/10

#
Asteroid class:
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * A rock in space
 * 
 * @author Poul Henriksen
 */
public class Asteroid extends SmoothMover
{
    /** Size of this asteroid */
    private int size;
    
    /** When the stability reaches 0 the asteroid will explode */
    private int stability;
    
    
    public Asteroid()
    {
        this(50);
    }
    
    public Asteroid(int size)
    {
        super(new Vector(Greenfoot.getRandomNumber(360), 2));
        setSize(size);
    }
    
    public Asteroid(int size, Vector speed)
    {
        super(speed);
        setSize(size);
    }
    
    public void act()
    {         
        move();
        intersecting();
    }

    /**
     * Set the size of this asteroid. Note that stability is directly
     * related to size. Smaller asteroids are less stable.
     */
    public void setSize(int size) 
    {
        stability = size;
        this.size = size;
        GreenfootImage image = getImage();
        image.scale(size, size);
    }

    /**
     * Return the current stability of this asteroid. (If it goes down to 
     * zero, it breaks up.)
     */
    public int getStability() 
    {
        return stability;
    }
    
    /**
     * Hit this asteroid dealing the given amount of damage.
     */
    public void hit(int damage) {
        stability = stability - damage;
        if(stability <= 0) 
            breakUp ();         
    }
    
    /**
     * Break up this asteroid. If we are still big enough, this will create two
     * smaller asteroids. If we are small already, just disappear.
     */
    private void breakUp() 
    {
      
        Greenfoot.playSound("Explosion.wav");
        
        if(size <= 16) 
        {
            getWorld().removeObject(this);
        ((Space)getWorld()).countScore(10);
        }
        else 
        {
            int r = getMovement().getDirection() + Greenfoot.getRandomNumber(45);
            double l = getMovement().getLength();
            Vector speed1 = new Vector(r + 60, l * 1.2);
            Vector speed2 = new Vector(r - 60, l * 1.2);        
            Asteroid a1 = new Asteroid(size/2, speed1);
            Asteroid a2 = new Asteroid(size/2, speed2);
            getWorld().addObject(a1, getX(), getY());
            getWorld().addObject(a2, getX(), getY());        
            a1.move();
            a2.move();
        ((Space)getWorld()).countScore(25);
            getWorld().removeObject(this);
        }
    }
    
    public void intersecting() {
        Actor rocket = getOneIntersectingObject(Rocket.class);
        if(rocket != null) {
            Actor Explosion = new Explosion();
            getWorld().addObject(Explosion,getX(), getY());
            ((Space)getWorld()).gameOver();
            Greenfoot.playSound("MetalExplosion.wav");
            getWorld().removeObject(rocket);
            getWorld().removeObject(this);
        }
    }
}
blangley83 blangley83

2014/7/10

#
java.lang.NullPointerException at Asteroid.breakUp(Asteroid.java:82) at Asteroid.hit(Asteroid.java:67) at Bullet.checkAsteroidHit(Bullet.java:51) at Bullet.act(Bullet.java:39) the error after reproduced with the above code.
danpost danpost

2014/7/10

#
Swap lines 81 and 82 ('getWorld', line 82, returns 'null' after the actor is removed from the world, line 81).
blangley83 blangley83

2014/7/10

#
Wow. Just wow. Thank you, Dan!
You need to login to post a reply.
1
2