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

2017/2/26

Need help with counting objects

TheMemeMachine TheMemeMachine

2017/2/26

#
Hello, Im just starting with Greenfoot, and I am making a basic game in wich i want to have a certain number of apples fall down from the top to the bottom of the world, and when they hit the bottom they disapear. The thing is, I want to make it so when the total number of apples on the screen is less than lets say 6, a new number of apples spawn so the total number of apples is equal or greater than 6. How do i count the total number of apples on the screen at any given time? The plan is to use the counting command within an if statement Thanks in advance
danpost danpost

2017/2/26

#
TheMemeMachine wrote...
How do i count the total number of apples on the screen at any given time? The plan is to use the counting command within an if statement
You want something like this:
1
if (getObjects(Apple.class).size() < 6)
in your World subclass act method.
TheMemeMachine TheMemeMachine

2017/2/26

#
danpost wrote...
TheMemeMachine wrote...
How do i count the total number of apples on the screen at any given time? The plan is to use the counting command within an if statement
You want something like this:
1
if (getObjects(Apple.class).size() < 6)
in your World subclass act method.
Thank you it worked wonderfully. Now, in my program there is a fly that eats the apples and when it colides with a bee it should dissapear and it lives count should be decreased by one and then it should respawn. How do i make it respawn in its startring position and how do i make the lives counter decresae? I already made the counter appear on my world and declared the int lives=3; variable
danpost danpost

2017/2/26

#
TheMemeMachine wrote...
How do i make it respawn in its startring position and how do i make the lives counter decresae? I already made the counter appear on my world and declared the int lives=3; variable
Do not remove the actor from the world. Just have it move to its starting position while decreasing the lives counter (unless the counter value is zero). For further help on this, be more specific and show codes.
TheMemeMachine TheMemeMachine

2017/2/26

#
danpost wrote...
TheMemeMachine wrote...
How do i make it respawn in its startring position and how do i make the lives counter decresae? I already made the counter appear on my world and declared the int lives=3; variable
Do not remove the actor from the world. Just have it move to its starting position while decreasing the lives counter (unless the counter value is zero). For further help on this, be more specific and show codes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public int score=0;
    public int lives=3;
     
    public World()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1500, 800, 1);
         
        addObject(new fly(), 50, 750);
        addObject(new bee(), 1400, 750);
        addObject(new bee(), 1400, 750);
        addObject(new bee(), 1400, 750);
        int i=0;
        while (i<6){
        addObject(new Apple(), Greenfoot.getRandomNumber(1500)-50,10);
        i=i+1;}
        ShowScore();
        ShowLives();
        if(lives==0){
        Greenfoot.stop();}
    }
    public void ShowScore(){
     showText("Score:"+score,50,50);}
      
    public void ShowLives(){
         showText("Lives:"+score,50,70);}
 
public void act(){
if (getObjects(Apple.class).size()<6){
   addObject(new Apple(), Greenfoot.getRandomNumber(1500)-50,10);}
     if(lives==0){
        Greenfoot.stop();}
}
this is the code of my world subclass, how do i integrate a working score counter and lives counter into this, that is the question. If you need any of the other actor classes source code, just say so and i will send them. Thank you for your replies
danpost danpost

2017/2/26

#
First, change the name of your World subclass. Use 'MyWorld' or "Welt' (german for 'world') -- anything but World or Object or any other class name that is already being used in your project. To decrease the lives from an actor class, you can just use:
1
2
3
MyWorld mw = (MyWorld)getWorld();
mw.lives--;
mw.ShowLives();
Similar code can be used for the score.
TheMemeMachine TheMemeMachine

2017/2/26

#
danpost wrote...
First, change the name of your World subclass. Use 'MyWorld' or "Welt' (german for 'world') -- anything but World or Object or any other class name that is already being used in your project. To decrease the lives from an actor class, you can just use:
1
2
3
MyWorld mw = (MyWorld)getWorld();
mw.lives--;
mw.ShowLives();
Similar code can be used for the score.
Sorry, i dont understand what you did there. Where do I write this code? The lives counter is supposed to decrease in value when my fly class is eaten by the bee class, i dont see you use them in your example. Could you dumb it down for me a bit, im really just starting with greenfoot. I changed the World subclass name to Svet(Serbian for world)
danpost danpost

2017/2/26

#
If your World subclass name is Svet, then the code above would be:
1
2
3
Svet svet = (Svet)getWorld(); // gets reference to World object
svet.lives--; // decreases lives
svet.ShowLives(); // updates lives display
I did not put it with the code where bee eats fly. You need to do that This is just to decrease the counter and update the display. You can put it with your code where a bee eats the fly.
TheMemeMachine TheMemeMachine

2017/2/26

#
danpost wrote...
If your World subclass name is Svet, then the code above would be:
1
2
3
Svet svet = (Svet)getWorld(); // gets reference to World object
svet.lives--; // decreases lives
svet.ShowLives(); // updates lives display
I did not put it with the code where bee eats fly. You need to do that This is just to decrease the counter and update the display. You can put it with your code where a bee eats the fly.
But the code where the bee eats fly is in the bug subclass of the actor superclass. Does that mean that I am supposed to write your code in the world sublass act() method or where if not there?
danpost danpost

2017/2/26

#
TheMemeMachine wrote...
But the code where the bee eats fly is in the bug subclass of the actor superclass. Does that mean that I am supposed to write your code in the world sublass act() method or where if not there?
The only place the code will work is in an Actor subclass. The 'getWorld' method is an Actor class method and will not work in any method of a World subclass.
Nosson1459 Nosson1459

2017/2/27

#
TheMemeMachine wrote...
Does that mean that I am supposed to write your code in the world sublass act() method or where if not there?
If you write that code in the act method then the lives will be decreased once every act cycle which means that after a few seconds the lives will be -500. That code you put in the place that you want the lives to be decreased (same as where you put the code for re-setLocation of the fly).
You need to login to post a reply.