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

2017/10/31

Help! Can't display a message

twistedninja42 twistedninja42

2017/10/31

#
I've been trying everything I can and can't seem to display a message that says "Game Over" Once only one type of actor is on screen. For example, I want to display "Game over. Crabs win!" If only crabs are left on screen. Same thing for worms, and Lobsters. Any help?
danpost danpost

2017/11/1

#
You will first need to check if a game over actor is in the world. Then, if not, get the size of all lists needed of different actors that could win. If you have n number of lists, you will have n different checks consisting of n-1 size checks to determine if the one type not checked wins. The check made is whether the list is empty or not. In other words, if all but one list is empty, the non-empty list is the type that wins (but you have to check individually for each type that can win). For a scenario where 3 types of actors, ActorA, ActorB, and ActorC, could win:
// in world act method
int countA = getObjects(ActorA.class).size();
int countB = getObjects(ActorB.class).size();
int countC = getObjects(ActorC.class).size();
if (countA == 0 && countB == 0) gotWinner("C");
if (countA == 0 && countC == 0) gotWinner("B");
if (countB == 0 && countC == 0) gotWinner("A"):
A 'gotWinner' method can be created to display the appropriate message depending on the value passed to it.
twistedninja42 twistedninja42

2017/11/2

#
Well how exactly would I display a message? There's no "displayMessage", I don't think
danpost danpost

2017/11/2

#
twistedninja42 wrote...
Well how exactly would I display a message? There's no "displayMessage", I don't think
Create a subclass of Actor for the game over object. Add a constructor to create the image with text required. Have the 'gotWinner' create an object from the class and add it into the world.
You need to login to post a reply.