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

2014/10/25

Please help, I want to collect all objects from a certain class and check a specific value of a variable..

tummie555 tummie555

2014/10/25

#
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");
        }
    }
}
tummie555 tummie555

2014/10/25

#
Help please... I can't get no sleep with this issue unsolved... : |
tummie555 tummie555

2014/10/25

#
omg
danpost danpost

2014/10/25

#
Instead of constantly calculating, why not just keep track of the number. Add a static field that keeps tally on how many are at StatusA state. That means the field should be added to when a Student object is created or when paper hits that student and subtracted from when the student becomes non-StatusA. The difference between the number of students in the world and how many are StatusA will give the number that are not StatusA (this would be used in the 'act' method of your subclass of World). Btw, you could also add another static field for the total number of students in the Student class (incremented each time a Student object is created) so the world object does not have to count them every act cycle.
tummie555 tummie555

2014/10/25

#
Altered methods in Student class:

public void changeStatus()
    {
     if (Greenfoot.getRandomNumber(2000) < 3 && getImage() != statusB && getImage() != statusC)
     {
         Space klaslokaal = (Space) getWorld();
         Regel regel = klaslokaal.getRegel();
         regel.checkAantal(1);
         
         int status = Greenfoot.getRandomNumber(2);
          if (status == 0)
          {
              setImage(statusB);
          }
          else {
              setImage(statusC);
          }
     }
    }
    
    
public void hit()
    {
     if (getImage() == statusA || getImage() == boos)
     {
         irritatiefactor++;
         Greenfoot.playSound("hey.wav");
         setImage(boos); 
     }
     else {
         setImage(geschrokken);
         Greenfoot.playSound("opletten.wav");
         
         Space klaslokaal = (Space) getWorld();
         Regel regel = klaslokaal.getRegel();
         regel.checkAantal(-1);
     }
    }




added to World class:

private Regel regel1;

public Space()
    { 
regel1 = new Regel();
    }

public Regel getRegel()
    {
        return regel1;
    }




Extra class (Regel):

public class Regel extends Actor
{
    private int aantal = 0;
    

public void checkAantal(int a )
    { 
        aantal = aantal + a;
        if (aantal >= 6)
        {
            Greenfoot.stop();
            Greenfoot.playSound("gameover.wav");
        }
    }
}


Thanks Dan, now i can rest. I made an extra class, added a methode that alters the counter ( Minus for when the statusA is recovered, Plus for when it is changed to B or C. ) It was very tough to figure out how to access this method in this new class, but i followed one of the tutorials, and created all these references from the student class and declared a variable in the world class wich contained a instance of the extra class. Still don't exactly know what I am talking about, but it works. Thanks again for your reply.
danpost danpost

2014/10/26

#
Creating a new object type for something that can be dealt with by using a simple int field seems a bit much. And, unless you are adding the Regel object created into the world, the class does not have to extend Actor (you can either remove 'extends Actor' altogether or change it to 'extends Object').
You need to login to post a reply.