I am working on this project for school, so far so good. But now I've stumbled upon this tricky (for me) method .I need to collect all objects from a certain class and check a specific value of a variable. A little background information: the game is about an classroom where the teacher can throw a piece of paper towards students in the class. The students randomly change "state", that is; their image changes. If the paper hits the student, it returns to their original state.
Now about this "tricky" method; I need a rule that constantly calculates how many objects from the Student.class are set to GreenfootImage variable: StatusB and/or StatusC. If there are too many compared to all objects of Student.class, the game has to stop. How and where do I write the code to accomplish this??
Hope it is easy to solve for you, because I've tried for many many hours. ;)
Here is the code from Student.class:
public class Student extends Actor { private GreenfootImage statusA; private GreenfootImage statusB; private GreenfootImage statusC; private GreenfootImage geschrokken; private GreenfootImage boos; private int irritatiefactor; private int teller; public Student() { statusA = new GreenfootImage("studenten2.png"); statusB = new GreenfootImage("afgeleid.png"); statusC = new GreenfootImage("slapen.png"); boos = new GreenfootImage("boos.png"); geschrokken = new GreenfootImage("geschrokken.png"); setImage(statusA); irritatiefactor = 0; teller = 0; } /** * Act - do whatever the Studenten wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { changeStatus(); boos(); if (getImage() == boos || getImage() == geschrokken) { teller++; if (teller > 25) { setImage(statusA); teller = 0; } } } /** * Studenten veranderen willekeurig van staat. */ public void changeStatus() { if (Greenfoot.getRandomNumber(2000) < 3 && getImage() != statusB && getImage() != statusC) { int status = Greenfoot.getRandomNumber(2); if (status == 0) { setImage(statusB); } else { setImage(statusC); } } } /** * Keer terug naar oorspronkelijke staat wanneer geraakt. */ public void hit() { if (getImage() == statusA || getImage() == boos) { irritatiefactor++; Greenfoot.playSound("hey.wav"); setImage(boos); } else { setImage(geschrokken); Greenfoot.playSound("opletten.wav"); } } /** * Als de student 3x geraakt wordt terwijl hij al oplet, dan gaat hij naar de decaan en stopt het spel. */ public void boos() { if (irritatiefactor >= 3) { Greenfoot.stop(); Greenfoot.playSound("gameover.wav"); } } }