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

2012/11/14

Stopping when a score is reached.

Penguins_and_Orbs Penguins_and_Orbs

2012/11/14

#
I am trying to set it so that my game will stop when 10 orbs are eaten. There are two players, and therefore two seperate score counters. I want the World class to read these two counters, so that when added up they equal ten. How would I go about doing this?
Zamoht Zamoht

2012/11/14

#
You could make a static integer which counts for all objects of that class or you could use the "getObjects(java.lang.Class cls)" method to get the objects containing the values and then add them together.
Penguins_and_Orbs Penguins_and_Orbs

2012/11/21

#
How exactly would I make a static integer? Would I put the two classes in a method in something like the world class?
danpost danpost

2012/11/21

#
Let me ask, first. - do you have references to the 2 players in the world class (and their names are?) - do you have references to the counters in the players' classes (and they are called?) - do you have methods to return the counter object from the players' classes (method name?) - do you have a method in the counter class to return the value (method name?)
Penguins_and_Orbs Penguins_and_Orbs

2012/11/21

#
-I have the player names (Penguin and Bee) in one of the methods on the World class, where if they're both dead, the game stops. But apart from that, no. -I have this script in the player classes:
  
 private void eat()
   {
       Orb orb = (Orb) getOneIntersectingObject(Orb.class);
       if (orb == null) return;
       
       getWorld().removeObject(orb);
       if (getWorld().getObjects(Score.class).isEmpty()) return; 
       Score scr = (Score) getWorld().getObjects(Score.class).get(0);
       scr.bump();
    }
- I don't think so. - Yes: public int getScore()
danpost danpost

2012/11/21

#
If they are the only two counter objects in the world, you could use the following:
public int getScoreSum()
{
    int sum=0;
    for(Object obj : getObjects(Score.class))
    {
        Score score=(Score) obj;
        sum+=score.getScore(); // or whatever
    }
    return sum;
}
danpost danpost

2012/11/21

#
It appears you do not have one counter for the Penguin and one counter for the Bee (you may have created two counters, but who knows which one 'getWorld().getObjects(Score.class).get(0)' will return. They need be kept seperate, somehow. Maybe have each actor (the Penguin and the Bee) create their own instance of a counter
// for both the Penguin class and the Bee class
// instance field (or variable)
private Score score = new Score();
// with a method to get the score object
public Score getScoreObj()
{
    return score;
}
Then in the world class
// with instance fields
Penguin penguin;
Bee bee;
// after (in the world constructor)
penguin = new Penguin(); // in Bee repeat: bee = new Bee(); 
addObject(penguin, pX, pY); // adds the Penguin object
// put this
addObject(penguin.getScoreObj(), sX, sY); // adds the Score object
// repeat the above for the Bee and its Score object
// Then to check the sum, in world 'act'
if (penguin.getScoreObj().getScore()+bee.getScoreObj().getScore()==10) ...
This would mean you do not need the 'getScoreSum' method above and you can change your 'eat' method to
private void eat()
{
    Orb orb = (Orb) getOneIntersectingObject(Orb.class);
    if (orb == null) return;
    getWorld().removeObject(orb);
    score.bump();
}
Penguins_and_Orbs Penguins_and_Orbs

2012/11/27

#
Is the middle bit all supposed to go into the world class?
You need to login to post a reply.