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

2020/4/14

Counter Value help

NewUser1201 NewUser1201

2020/4/14

#
So in my game, I have a red and blue balloon, when you click on the red balloon a timer appears and it goes up by 1 every 750ms, when you click on the blue balloon it starts at 20 and goes down by 1 every 1250ms, now I want to change the world when the counter values are the same. However, I have no clue where to put this piece of code and how to access the counter values since they are in different actors. Also since I can click on either balloon at different times the equivalent value won't always be the same (sry if this may sound weird or it was too much info I'm still new to all of this and didn't know what info is needed to solve this).
danpost danpost

2020/4/14

#
Use an act method in your World subclass to compare and respond to equal counter values.
NewUser1201 NewUser1201

2020/4/14

#
This may sound dumb, but how would I compare the two counters, like what would I write to say if CR and CB (name of the counters) are equal switch worlds, I know how to switch the worlds just not sure how to compare the two counters
danpost danpost

2020/4/14

#
NewUser1201 wrote...
This may sound dumb, but how would I compare the two counters, like what would I write to say if CR and CB (name of the counters) are equal switch worlds, I know how to switch the worlds just not sure how to compare the two counters
Show counter class codes. Show World subclass codes also.
NewUser1201 NewUser1201

2020/4/14

#
public class MyWorld extends World { /** * Constructor for objects of class MyWorld. * */ public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); addObject(new BlueButton(), 400, 200); addObject(new RedButton(), 200, 200); } public void act() { } } public class RedButton extends Actor { Counter cr = new Counter(); SimpleTimer t = new SimpleTimer(); public RedButton() { t.mark(); } /** * Act - do whatever the RedButton wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (Greenfoot.mouseClicked(this)) { MouseInfo mouseInfo = Greenfoot.getMouseInfo(); int x = mouseInfo.getX(); int y = mouseInfo.getY(); getWorld().addObject(cr, 200, 160); cr.setValue(0); } if (t.millisElapsed() > 750) { cr.add(1); t.mark(); } } } public class BlueButton extends Actor { Counter cb = new Counter(); SimpleTimer t = new SimpleTimer(); public BlueButton() { t.mark(); } /** * Act - do whatever the BlueButton wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (Greenfoot.mouseClicked(this)) { MouseInfo mouseInfo = Greenfoot.getMouseInfo(); int x = mouseInfo.getX(); int y = mouseInfo.getY(); getWorld().addObject(cb, 400, 160); t.mark(); cb.setValue(20); } if (t.millisElapsed() > 1250) { cb.add(-1); t.mark(); } } }
danpost danpost

2020/4/14

#
Okay. The following should ask if the values of the buttons are the same:
1
if (((BlueButton)getObjects(BlueButton.class).get(0)).cb.getValue() == ((RedButton)getObjects(RedButton.class).get(0)).cr.getValue())
However, it is a bit awkward and lengthy. I would suggest having the world retain specific references to the button counters:
1
2
3
4
5
6
7
8
9
10
11
12
private Counter redCounter, blueCounter;
 
public MyWorld()
{
    super(600, 400, 1)
    BlueButton button = new BlueButton();
    blueCounter = button.cb;
    addObject(button, 400, 200);
    button = new RedButton();
    redCounter = button.cr;
    addObject(button, 200, 200);
}
then the previous line would simply be:
1
if (blueCounter.getValue() == redCounter.getValue())
danpost danpost

2020/4/14

#
I guess you will have to check this as well:
1
if (getObjects(Counter.class).size() == 2)
before comparing the counter values as (I presume) both counters need to be in the world.
danpost danpost

2020/4/14

#
Correction:
1
2
3
4
5
6
7
8
9
10
public MyWorld()
{
    super(600, 400, 1)
    BlueButton buttonB = new BlueButton();
    blueCounter = buttonB.cb;
    addObject(buttonB, 400, 200);
    RedButton buttonR = new RedButton();
    redCounter = buttonR.cr;
    addObject(buttonR, 200, 200);
}
NewUser1201 NewUser1201

2020/4/14

#
It sort of works, the only problem now is that when I press play it immediately brings me to the second world without even clicking the balloons
danpost danpost

2020/4/14

#
NewUser1201 wrote...
It sort of works, the only problem now is that when I press play it immediately brings me to the second world without even clicking the balloons
Did you notice this:
danpost wrote...
I guess you will have to check this as well:
1
if (getObjects(Counter.class).size() == 2)
before comparing the counter values as (I presume) both counters need to be in the world.
NewUser1201 NewUser1201

2020/4/14

#
That fixed it, thank you for all your help
You need to login to post a reply.