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

2015/10/29

New to Java, using the counter to go to level2

johnb johnb

2015/10/29

#
Hi this is my first program in java , How do I make the counter when it reaches "100" trigger to go to level 2, I have the counter set up under actor in world 1 . Any help would be grateful, thank you in advance
kev kev

2015/10/31

#
please help i want to know the same thing!
Super_Hippo Super_Hippo

2015/10/31

#
1
if (counter==100) Greenfoot.setWorld(new Level2());
johnb johnb

2015/10/31

#
Thanks Super Hippo, I can`t see where to call this method in the actor class. maybe im not making sense !
danpost danpost

2015/10/31

#
I think what you need is to put the following in your World1 act method:
1
if (counter.getValue() == 100) Greenfoot.setWorld(new Level2());
This is assuming that you have a 'Counter counter' field declared within that class that is assigned the Counter object you created and added into the world.
Super_Hippo Super_Hippo

2015/10/31

#
In your Counter class, you probably have methods to in-/decrease the value. This method could look like this:
1
2
3
4
5
6
7
public void add(final int s)
{
    value += s;
    //here you can add the line:
    if (value > 99) Greenfoot.setWorld(new Level2());
    updateImage();
}
Or, if you always add 1.
1
2
3
4
5
6
public void add()
{
    value++; // or value-- if the value should decrease from something to 100 (probably not though)
    if (value == 100) Greenfoot.setWorld(new Level2());
    updateImage();
}
danpost danpost

2015/10/31

#
@Super_Hippo, your suggestion would work provided you only ever create the one counter whose value will lead to a Level2 world. In more general terms, by placing the code to proceed to the next world in the Counter class, you are taking away the re-usability of the Counter class. The Counter class provided with greenfoot was not intended to be modified. Multiple counters can be created from the class when not modified. That is, you can have one or more of any type counters including scores, health value displays, timers etc, -- all from the same class. Placing the code to change worlds in the subclass of World that preceeds Level2 is more logical and preserves the Counter class.
kev kev

2015/11/11

#
thanks but one problem if I try to make a counter it doesn't count I have the method to make my actor count what code do I need?
danpost danpost

2015/11/11

#
@kev, maybe you should look over my Value Display Tutorial scenario. It contains pretty much all you need to know about counters.
kev kev

2015/11/12

#
I'll check it out when I'm on computer it doesn't work on iPad
You need to login to post a reply.