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

2017/2/13

Lives counter

dillyduck dillyduck

2017/2/13

#
hello. i want to make a lives timer so that when an asteroid hits earth, earth loses a life until it reacher zero, then Greenfoot should stop. any advise?
Nosson1459 Nosson1459

2017/2/13

#
Have a variable that is equal to the amount of lives you want earth to have, each time an asteroid hits earth decrease the lives variable by one if lives equals zero do Greenfoot.stop();.
dillyduck dillyduck

2017/2/13

#
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
private int lives;
 
public Space()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1050, 400, 1);
        prepare();
        score = 0;
        showScore();
        time = 2000;
        showTime();
        lives = 3;
        showLives();
    }
 
/**
     * If an Asteroid hits Earth, remove life until 0
     */
    private void removeLives()
       {
          lives--;
          showLives();
          if( lives == 0 )
             {
             Greenfoot.stop();   
             showEndMessage();
             
       }
    private void showLives()
       {
          showText("Lives:" + lives, 400, 350);   
       }
I have this so far. what exactly to i put or correct in order for the counter to work.
Nosson1459 Nosson1459

2017/2/13

#
The code that was showed looks fine to me. What doesn't work exactly, otherwise there's no way that I can help you.
dillyduck dillyduck

2017/2/13

#
where do i put the BigAsteroid and SmallAsteroid to collide with the Earth in order to make the lives counter go down
Nosson1459 Nosson1459

2017/2/13

#
In your earth code do:
1
2
3
4
5
6
if(isTouching(BigAsteroid.class))
{
    Space space = (Space)getWorld();
    space.removeLives();
    removeTouching(BigAtseroid.class);
}
and use the same code for the SmallAsteroid.
dillyduck dillyduck

2017/2/13

#
got it. thanks a million
dillyduck dillyduck

2017/2/13

#
im making final touches and when i put everything to gather it gives me an error on line 4 that says removeLines() has private access in Space. Any idea on how to fix?
danpost danpost

2017/2/13

#
dillyduck wrote...
im making final touches and when i put everything to gather it gives me an error on line 4 that says removeLines() has private access in Space. Any idea on how to fix?
Make it 'public' instead of 'private'.
Nosson1459 Nosson1459

2017/2/13

#
danpost wrote...
Make it 'public' instead of 'private'.
"it". Basically the method should be:
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
 * If an Asteroid hits Earth, remove life until 0
 */
public void removeLives()
{
    lives--;
    showLives();
    if( lives == 0 )
    {
        Greenfoot.stop();   
        showEndMessage();
    }
}
danpost danpost

2017/2/14

#
Nosson1459 wrote...
danpost wrote...
Make it 'public' instead of 'private'.
"it".
What else could "it" be?
dillyduck wrote...
it gives me an error on line 4 that says removeLines() has private access in Space.
Nosson1459 Nosson1459

2017/2/14

#
I didn't say it could be anything else but the question was "Any idea on how to fix?" meaning the problem so I thought that by him "it" means the error/problem so he has to edit that method not just edit it.
You need to login to post a reply.