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

2023/6/15

I'm having trouble with using two separate counters.

Wish10940 Wish10940

2023/6/15

#
I'm creating a game where a bee flies around and catches flowers which increases the pollen count and this can be stored in the hive, putting the pollen's number into the hive and putting the hive to zero. Here's what i have for emptying the pollen into the hive: public void empty() { Actor hive; hive = getOneObjectAtOffset (0,0,hive.class); if (hive!=null) { BG bg = (BG) getWorld(); counter counter1 = bg.getCounter(); hive_counter hive_counter = new hive_counter(); hive_counter counter2 = bg.getPollen(); int pollenCount = counter1.getPollen(); hive_counter.stored = pollenCount; Greenfoot.playSound("poll.wav"); counter1.pollen = 0; } } it runs with no errors, but does not add the pollen count into the hive counter Here's my code for adding to the pollen counter when hitting a yellow flower: Actor yellow; yellow = getOneObjectAtOffset (0,0,yellow.class); if (yellow!=null) { World myWorld = getWorld(); myWorld .removeObject(yellow); BG bg = (BG) getWorld(); counter counter = bg.getCounter(); counter.addPollen(1); Greenfoot.playSound("poll.wav"); int randX = Greenfoot.getRandomNumber(getWorld().getWidth()); int randY = Greenfoot.getRandomNumber(getWorld().getHeight()); myWorld.addObject(new yellow(), randX, randY);
danpost danpost

2023/6/16

#
Wish10940 wrote...
I'm creating a game where a bee flies around and catches flowers which increases the pollen count and this can be stored in the hive, putting the pollen's number into the hive and putting the hive to zero. << Codes Omitted >>
Here is some of your code (lines numbered):
public void empty()
{
    Actor hive;
    hive = getOneObjectAtOffset (0,0,hive.class);
    if (hive!=null)
    {
        
        BG bg = (BG) getWorld();
         
        counter counter1 = bg.getCounter();
       
        hive_counter hive_counter = new hive_counter();
        hive_counter counter2 = bg.getPollen();
        int pollenCount = counter1.getPollen();
        hive_counter.stored = pollenCount;
        Greenfoot.playSound("poll.wav");
        
        counter1.pollen = 0;
    }
}
Anything created or declared within a method is temporary unless some link is created with some object. If no reference is created to the created object or variable, then it is lost when the method is completed. Line 12 creates a hive_counter object, assigning it to a local object variable you declare as hive_counter. Line 15 does set the value of stored in this new object, but that is all the method does with it. It is not added to the world, nor is it stored in an instance field, both of which would create a reference to the new object. As you have it, your method, in essence, does this:
public void empty()
{
    Actor hive = getOneObjectAtOffset(0, 0, hive.class);
    if (hive != null)
    {
        ((BG) getWorld()).getCounter().pollen = 0;
    }
}
I am not so sure that you should be having all your bees add to the same int field, in the world's counter object, to begin with. Each bee should keep its own pollen count until it reaches the hive; then, transfer (add) it the the hive's pollen count and clear its own.
Wish10940 Wish10940

2023/6/16

#
I only have one bee character, so it's storing it into the pollen counter with each flower, each one with a different amount of pollen. this works fine, however once it gets to moving the integer stored in the pollen counter to the hive counter to "empty the pollen into the hive" it doesn't work. any tips on what code to do for that?
danpost danpost

2023/6/17

#
Wish10940 wrote...
I only have one bee character, so it's storing it into the pollen counter with each flower, each one with a different amount of pollen. this works fine, however once it gets to moving the integer stored in the pollen counter to the hive counter to "empty the pollen into the hive" it doesn't work. any tips on what code to do for that?
Try this:
public void empty()
{
    Actor hive = getOneObjectAtOffset(0, 0, hive.class);
    if (hive != null)
    {
        BG bg = (BG)getWorld();
        bg.getPollen().stored += bg.getCounter().pollen;
        bg.getCounter().pollen = 0;
        Greenfoot.playSound("poll.wav");
    }
}
Wish10940 Wish10940

2023/6/18

#
I've got this error after touching the hive
java.lang.NullPointerException
	at bee.empty(bee.java:116)
	at bee.act(bee.java:20)
	at greenfoot.core.Simulation.actActor(Simulation.java:567)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:530)
	at greenfoot.core.Simulation.runContent(Simulation.java:193)
	at greenfoot.core.Simulation.run(Simulation.java:183)
it says it's a problem with the line bg.getPollen().stored += bg.getCounter().pollen;
danpost danpost

2023/6/19

#
Wish10940 wrote...
I've got this error after touching the hive << Error Trace Omitted >> it says it's a problem with the line bg.getPollen().stored += bg.getCounter().pollen;
Either the bee is being removed from the world before the act method calls this empty method or one of your world methods (getPollen or getCounter) is not returning a counter object.
You need to login to post a reply.