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

2019/4/28

Explode the Rocket

Celesterra Celesterra

2019/4/28

#
I have been trying to make a health function in this scenario. I got it all to work, except that I can't make an explosion occur when the rocket has been hit 5 times. Here is the code I can't get to execute (due to NullPointerException error):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Actor rocket = getOneIntersectingObject(Rocket.class);
 if(rocket != null)
{
    if(((Galaxy) getWorld()).hits < 5)
    {
        Galaxy world = (Galaxy)getWorld();
        world.updateHealth();
        Greenfoot.playSound("crunch.mp3");
        getWorld().removeObject(this);
    }
                 
    else if(((Galaxy) getWorld()).hits == 5)
    {
        getWorld().addObject(new Explosion(), getX(), getY());
        getWorld().removeObject(rocket);
        getWorld().removeObject(this);
        Galaxy world = (Galaxy)getWorld();
        world.updateHealth();
    }
}
This is the asteroid class. And the line that makes the error is line 12. I don't understand why the first if statement works, but the second one doesn't.
Super_Hippo Super_Hippo

2019/4/28

#
Change the order. Remove the object (this) at the end.
TheGoldenProof TheGoldenProof

2019/4/28

#
What Super_Hippo said. In line 17 you try to get the World of your object, but its been removed from the world, so its not in the world. You need to remove it from the world after calling any functions that require it to be in world.
Celesterra Celesterra

2019/4/28

#
I did that and no longer get the error, but the else if statement still won't execute.
Super_Hippo Super_Hippo

2019/4/29

#
Do you change the "hit" variable somewhere?
danpost danpost

2019/4/29

#
Celesterra wrote...
the line that makes the error is line 12. I don't understand why the first if statement works, but the second one doesn't.
I doubt seriously that line 12 caused the error. Clear your terminal, recreate the error, the post both the terminal output along with the current code for review.
Celesterra Celesterra

2019/4/29

#
For some reason, I can't get the error to appear... I don't know what caused it. Anyways, I will post all the code that was involved in the error (also, if anyone can tell me why the else if statement won't execute, that would be great!). Asteroid:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public class Asteroid extends Actor
{
    //Variables used later
    private int speed = 2;
     
    public void act()
    {
        setLocation(getX()-speed, getY());
         
        //Once an asteroid reaches the left edge, it will disappear
        if(getX()< speed)
        {
            getWorld().removeObject(this);
        }
         
        //If the asteroid collides with a rocket, decrease health and destroy asteroid or explode both
        else
        {
            Actor rocket = getOneIntersectingObject(Rocket.class);
            if(rocket != null)
            {
                if(((Galaxy) getWorld()).hits < 5)
                {
                    Galaxy world = (Galaxy)getWorld();
                    world.updateHealth();
                    Greenfoot.playSound("crunch.mp3");
                    getWorld().removeObject(this);
                }
                 
                else if(((Galaxy) getWorld()).hits == 5)
                {
                    getWorld().addObject(new Explosion(), getX(), getY());
                    getWorld().removeObject(rocket);
                    getWorld().removeObject(this);
                    Galaxy world = (Galaxy)getWorld();
                    world.updateHealth();
                }
            }
        }
    }
     
    public void explode()
    {
        for(int d=0; d<50; d++)
        {
            getWorld().addObject(new Debris(), getX(), getY());
        }
         
        Greenfoot.playSound("boom2.mp3");
        getWorld().removeObject(this);
    }
}
Health:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
public class Health extends Actor
{
    public Health()
    {
        //Creates the image of the health bar
        GreenfootImage img = new GreenfootImage(150, 20);
        img.setColor(new Color(34,196,19));
        img.fill();
        setImage(img);
    }   
     
    public void damage()
    {
        Galaxy world = (Galaxy)getWorld();
         
        //Recreates the image of the health bar when the rocket takes damage (depending on # of hits)
        if(world.hits == 1)
        {
            GreenfootImage img = new GreenfootImage(120, 20);
            img.setColor(new Color(184,247,27));
            img.fill();
            setImage(img);
        }
         
        if(world.hits == 2)
        {
            GreenfootImage img = new GreenfootImage(90, 20);
            img.setColor(new Color(255,250,0));
            img.fill();
            setImage(img);
        }
         
        if(world.hits == 3)
        {
            GreenfootImage img = new GreenfootImage(60, 20);
            img.setColor(new Color(255,165,0));
            img.fill();
            setImage(img);
        }
         
        if(world.hits == 4)
        {
            GreenfootImage img = new GreenfootImage(30, 20);
            img.setColor(new Color(255,0,0));
            img.fill();
            setImage(img);
        }
         
        if(world.hits == 5)
        {
            death();
        }
    }
     
    public void death()
    {
        //Resets hits and image of health bar and removes a life from life counter
        GreenfootImage img = new GreenfootImage(1, 1);
        img.setColor(Color.BLACK);
        img.fill();
        setImage(img);
             
        Galaxy world = (Galaxy) getWorld();
        world.updateLives();
        world.hits = 0;
        resetImage();
    }
     
    private void resetImage()
    {
        //Once hits resets to 0, change it to original image
        GreenfootImage img = new GreenfootImage(150, 20);
        img.setColor(new Color(34,196,19));
        img.fill();
        setImage(img);
    }
}
Galaxy:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
public class Galaxy extends World
{
    //Variables used later
    private Scoreboard sb;
    private Lives life;
    private Rocket rocket;
    public Asteroid rock;
    public Health hp;
    private LaserLimit limit;
    public int lasers = 30;
    public int hits = 0;
     
    public Galaxy()
    {
        // Create a new world with 800x600 cells with a cell size of 1x1 pixels.
        super(800, 600, 1);
        prepare();
    }
     
    private void prepare()
    {
        // Creates the black background of the galaxy
        GreenfootImage bg = new GreenfootImage(800, 600);
        bg.fill();
        setBackground(bg);
         
        //Spawns in the stars of the Galaxy
        addStars(800);
         
        //Spawns in the rocket
        addObject(new Rocket(), 75, getHeight()/2);
         
        //Spawns lasers underneath the rocket
        setPaintOrder(GameOver.class, Health.class, Rocket.class, Laser.class);
         
        //Prevents delay of animation
        Explosion.initializeImages();
         
        //Spawns in the scoreboard
        sb = new Scoreboard();
        addObject(sb, 70, 570);
         
        //Spawns in the life counter
        life = new Lives();
        addObject(life, 140, 570);
         
        //Spawns in the health bar
        hp = new Health();
        addObject(hp, 700, 570);
         
        //Spawns in a laser counter
        limit = new LaserLimit();
        updateLasers();
        addObject(limit, 210, 570);
    }
     
    public void updateScore()
    {
        sb.changeScore();
    }
     
    public void updateLasers()
    {
        GreenfootImage img = new GreenfootImage(100, 30);
        img.setColor(Color.WHITE);
        img.drawString("Lasers Left: " + (lasers == Integer.MAX_VALUE ? "∞" : lasers), 5, 25);
        limit.setImage(img);
    }
     
    public void updateLimit()
    {
        if (lasers == Integer.MAX_VALUE) return;
        lasers--;
        updateLasers();
    }
     
    public void noLimit()
    {
        lasers = Integer.MAX_VALUE;
        updateLasers();
    }
     
    public void updateHealth()
    {
        hits++;
        hp.damage();
    }
     
    public void updateLives()
    {
        //Changes the life counter and spawns in a new rocket after destroying it
        life.changeLives();
        addObject(new Rocket(), 75, getHeight()/2);
    }
     
    private void addStars(int n)
    {
        //Loop to add stars to the galaxy
        for(int s=0; s<n; s++)
        {
            int x = Greenfoot.getRandomNumber(getWidth());
            int y = Greenfoot.getRandomNumber(getHeight());
            addObject(new Star(), x, y);
        }
    }
     
    public void act()
    {
        //Spawns in asteroids at 0.5% of the time
        if(Greenfoot.getRandomNumber(1000) < 5)
        {
            addObject(new Asteroid(), getWidth()-5, Greenfoot.getRandomNumber(getHeight()));
        }
    }
}
danpost danpost

2019/4/29

#
In your Asteroids class, replace lines 22 to 37 with the following:
1
2
3
4
5
6
7
8
9
Galaxy world = (Galaxy)getWorld();
if(world.hits == 5)
{
    world.addObject(new Explosion(), getX(), getY());
    world.removeObject(rocket);
}
else Greenfoot.playSound("crunch.mp3");
world.removeObject(this);
world.updateHealth();
I doubt this will fix your issue, but once it is fixed, you will now avoid a different issue.
Celesterra Celesterra

2019/4/29

#
Does anyone have any idea on how I can get the if(world.hits == 5) to work? It STILL won't execute. I don't know why!
danpost danpost

2019/4/29

#
Celesterra wrote...
Does anyone have any idea on how I can get the if(world.hits == 5) to work? It STILL won't execute. I don't know why!
I can see why; however, fixing it may be a bit tricky. The reason why it does not execute is because hits is never 5 when that if statement is reached. Wait -- try checking for a value of 4 (since the hits field has not been bumped yet when the if statement is executed).
Celesterra Celesterra

2019/4/30

#
It works now! All I had to do was change it to equal 4 instead of 5. Thank you all!
You need to login to post a reply.