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

2012/10/17

Ending game when eaten

1
2
Penguins_and_Orbs Penguins_and_Orbs

2012/10/17

#
I have a two player game, and I am trying to make it end when not one, but both are eaten, in whatever order it happens. I put in the basic: public void lookForPlayers() { if { Greenfoot.stop(); } } But am unsure on how to make it return when both are gone. (This script is in the class which actually eats the players).
SPower SPower

2012/10/17

#
In which class is this method? And I advice this:
public void act()
{
     checkPlayers();
}

private void checkPlayers()
{
     if (getObjects(Player.class).isEmpty()) {
          Greenfoot.stop();
     }
}
do this in a subclass of world. Note that I used getObjects(Player.class) where Player is the class of the 2 players which can be killed.
SPower SPower

2012/10/17

#
And next time, if you want to add code in a discussion, do it between {code} and {/code} tags, and replace the { and } by .
Penguins_and_Orbs Penguins_and_Orbs

2012/10/17

#
   
public void lookForPlayers()
   {
       if 
       {
           Greenfoot.stop();
       }
    }
Ah... thanks. I'll try the code out.
Penguins_and_Orbs Penguins_and_Orbs

2012/10/17

#
When you say, (Player.class) represents the class of the two players, do you mean I should have one class that counts for both players?
SPower SPower

2012/10/17

#
You could do that, but you can also do it for both:
private void checkPlayers()
{
     if (getObjects(Player1.class).isEmpty() && getObjects(Player2.class).isEmpty()) {
          Greenfoot.stop();
     }
}
Penguins_and_Orbs Penguins_and_Orbs

2012/10/17

#
Oh yeah.. of course. :3
Penguins_and_Orbs Penguins_and_Orbs

2012/10/17

#
Maybe there's another script counteracting it, but it isn't working dispite being correct...
SPower SPower

2012/10/17

#
  • Did you put it in your world class?
  • Is it called every act cycle in the world subclass?
Penguins_and_Orbs Penguins_and_Orbs

2012/10/19

#
I put it in a new world class, and do you mean is it called as in is that it's name?
SPower SPower

2012/10/19

#
No, called like this:
checkPlayers();
from to call a method :)
Penguins_and_Orbs Penguins_and_Orbs

2012/10/19

#
Oh right, yeah it is.
SPower SPower

2012/10/19

#
Maybe you need to set a breakpoint on this line:
if (getObjects(Player1.class).isEmpty() && getObjects(Player2.class).isEmpty()) {
Penguins_and_Orbs Penguins_and_Orbs

2012/10/19

#
:/ Hasn't done anything. By the way, it didn't like the '&&' so i took it out, would that change anything?
SPower SPower

2012/10/19

#
Yes: you would get this:
if (getObjects(Player1.class).isEmpty() getObjects(Player2.class).isEmpty()) {
which won't compile. And the fact that it didn't do anything meant that the line wasn't executed, can you show me all the code of your world class?
There are more replies on the next page.
1
2