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

2015/12/17

Stopping the game when making contact with another character

Javanovice Javanovice

2015/12/17

#
I am trying to program the game to stop automatically when the bird actors hits the penguin actor, but I don't know how to do it. Here is my current code for the bird.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
private boolean canHitPengu()
    {
        Pengu  pengu = (Pengu) getOneObjectAtOffset(0, 0, Pengu.class);
        if(pengu != null)
        {
            return true;
        }
        else {
            return false;
        }
    }
     
    private void hitPengu()
    {
         if(Hit)
         {
              return;
         }
         Pengu  pengu = (Pengu) getOneObjectAtOffset(0, 0, Pengu.class);
    }
danpost danpost

2015/12/17

#
The second method given above really does not make any sense -- the 'Hit' field is not declared and the 'pengu' reference is lost as soon as it is created because the method ends right there. The first method is not needed as calling 'canHitPengu()' is exactly the same as calling 'isTouching(Pengu.class)'. Finally, there is no 'Greenfoot.stop()' command in the code to stop the game.
erdelf erdelf

2015/12/18

#
remove the first method and it is basically this
1
2
3
4
5
6
7
private void hitPengu()
{
     if(isTouching(Pengu.class))
     {
          Greenfoot.stop();
     }
}
You need to login to post a reply.