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

2019/2/6

Counter for lives not incrementing or decrementing.

1
2
Notted Notted

2019/2/6

#
I have a life counter. It's called aCounterLives. This is meant for the lives (numOfBuckets). I'm not why, but I think it's because of the counter's methods. This is the method that deals with the players lives. numOfBuckets is a dynamic variable for now, though I think it being set would likely fix the problem.
private void playerLives()
    {
      numOfBuckets = 3;
      boolean bombHasHitArea = ((MyWorld)getWorld()).getBoomArea().bombBoom();
      MyWorld kaboomWorld = (MyWorld) getWorld();
      aCounterLives bucketsCounter = kaboomWorld.getBucketsCounter();
       if (bombHasHitArea == true)
       {
           numOfBuckets = numOfBuckets - 1;
           bucketsCounter.decCount(1);
       }
    }
aCounterLives (whole class)
public class aCounterLives extends Actor
{
   private int totalCount = 0;
    /**
     * This is a counter. Counts up.
     * Most code is from https://www.greenfoot.org/doc/howto-1 
     */
    public aCounterLives ()
    {
        setImage(new GreenfootImage("0", 20, Color.BLACK, Color.WHITE));
    }
    public void bumpCount(int amount)
    {
        totalCount += amount;
        setImage(new GreenfootImage("" + totalCount, 20, Color.BLACK, Color.WHITE));
    }
    public void decCount(int amount)
    {
        totalCount -= amount;
        setImage(new GreenfootImage("" + totalCount, 20, Color.BLACK, Color.WHITE));
    }   
}
danpost danpost

2019/2/7

#
You will need to show the initialization and assignment of and the getter method for the aCounterLives object in your MyWorld class.
Notted Notted

2019/2/7

#
Here it is.
private aCounterLives bucketsCounter;
    public aCounterLives getBucketsCounter()
    {
        return bucketsCounter;
    }
Basically what I want to do is start with three lives, and then decrease the number of lives by one. The counter will record this.
danpost danpost

2019/2/8

#
Notted wrote...
Here it is. << Code Omitted >> Basically what I want to do is start with three lives, and then decrease the number of lives by one. The counter will record this.
How about the assigning of an object to the field and the command to create that object? All you have shown is the declaration of the field and the getter method for the value of the field.
Notted Notted

2019/2/8

#
I think I have even a bigger problem. My lives (numOfBuckets) won't decrement. The decrementation is in the boomArea class. It has a playerLives method (its return type is void, so maybe that's the problem?) :
private void playerLives()
    {
      MyWorld kaboomWorld = (MyWorld) getWorld();
      int playerLives = ((MyWorld)getWorld()).getABucket().getNumOfBuckets();
      GreenfootSound losingSound = new GreenfootSound("lose.wav");
       if (bombBoom())
       {
           playerLives--;
           //losingSound.play();
       }
    }
This does not work. Would this work?
 private int playerLives(int playersBuckets)
    {
      MyWorld kaboomWorld = (MyWorld) getWorld();
      int playerLives = ((MyWorld)getWorld()).getABucket().getNumOfBuckets();
      GreenfootSound losingSound = new GreenfootSound("lose.wav");
       if (bombBoom())
       {
           playerLives--;
           //losingSound.play();
       }
return playersBuckets;
    }
Amended act call:
playerLives(3);
danpost danpost

2019/2/8

#
danpost wrote...
How about the assigning of an object to the field and the command to create that object? All you have shown is the declaration of the field and the getter method for the value of the field.
Bump.
Notted Notted

2019/2/8

#
That's no longer our issue. I've removed all the references to aCounterLives.
danpost danpost

2019/2/8

#
Notted wrote...
That's no longer our issue. I've removed all the references to aCounterLives.
Alright.
I think I have even a bigger problem. My lives (numOfBuckets) won't decrement. The decrementation is in the boomArea class. It has a playerLives method (its return type is void, so maybe that's the problem?) : << Code Omitted >>
What is playerLives actually counting? Or, maybe (to get a better idea of what you are doing), the question should be: do you remove the player from the world each time a bomb hits it, or does it stay in the world?
Notted Notted

2019/2/8

#
It's a reference to NumOfBuckets. It's from the getter method getNumOfBuckets in the bucket class.
int playerLives = ((MyWorld)getWorld()).getABucket().getNumOfBuckets();
I remove a life everytime the bomb hits the boomArea. That's what the playerLives method is for.
danpost danpost

2019/2/8

#
Notted wrote...
It's a reference to NumOfBuckets. It's from the getter method getNumOfBuckets in the bucket class.
int playerLives = ((MyWorld)getWorld()).getABucket().getNumOfBuckets();
I remove a life everytime the bomb hits the boomArea. That's what the playerLives method is for.
Then -- the player stays in the world (until all 3 lives are lost)?
Notted Notted

2019/2/8

#
Yes. The game is meant to restart when the player loses all of their lives.
danpost danpost

2019/2/8

#
Notted wrote...
Yes. The game is meant to restart when the player loses all of their lives.
Okay. What is the difference between the two int variables playerLives andd playersBuckets?
Notted Notted

2019/2/8

#
The first one is a ref to numOfBuckets. The second is there to return how many lives the player has and use it.
danpost danpost

2019/2/8

#
Notted wrote...
The first one is a ref to numOfBuckets. The second is there to return how many lives the player has.
The calling method already knows the value it is passing (which is the exact same value that is being returned). I do not get the logic there.
Notted Notted

2019/2/8

#
Do you want me to remove the return type and call for the playerLives method?
There are more replies on the next page.
1
2