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

2012/9/13

Changing the lvls

1
2
3
Stephon231 Stephon231

2012/9/13

#
can some one give me a code that makes my main world reference a counter(leavesEaten) so that when the counter get to 50 that it changes the world i have tried putting this in the wombat but when the counter gets 50 on world2 it just resets world2. pls help.
MatheMagician MatheMagician

2012/9/13

#
I'm not sure what you're asking. If you're trying to set the world then say:
1
Greenfoot.setWorld(new World2());
If this isn't what you are asking for, you could post the code for the world2 class and that would help.
danpost danpost

2012/9/14

#
Why not put the code to change worlds in the Wombat class where you are changing the counter. After the call to eatLeaves() in the act method of the Wombat class, add the following snippet
1
2
3
4
5
if (leavesEaten == 50)
{
    Greenfoot.setWorld(new WorldName());
    return;
}
Just change 'WorldName' to the name of the world you want to go to. The 'return;' statement is there to skip the rest of the act() method code and to change worlds immediately.
danpost danpost

2012/9/14

#
Maybe you were not very clear as to what you want. Your post was a bit cryptic. Are you saying that you run your main world while the counter goes to 50, then world2 with the counter going to 50 , then world2 again? What exactly is it doing and what exactly do you want it to do?
Stephon231 Stephon231

2012/9/14

#
what i need is a code that gives a reference to the counter in the wombat so that when it gets to 50 it changes the world . if i use the world switch to worlds i can set the goal different for each lvl.
danpost danpost

2012/9/14

#
You could have a 'static' (class) field (public static int goal = 50;) set to the first goal amount (not neccessarily 50). Then set it to a new value before resetting the (same) world.
1
2
3
4
5
6
7
// in the Wombat class act method
if (leavesEaten == goal)
{
    goal = goal + 10; // increasing the goal (not neccessarily 10)
    Greenfoot.setWorld(new MainWorld());
    return;
}
Just replace 'MainWorld' with the name of your only game-running world.
Stephon231 Stephon231

2012/9/14

#
for goal i put the amount of leaves eaten
danpost danpost

2012/9/14

#
Is that a question? If so, no. For goal, you put the amount of leaves to be eaten before the next level.
Stephon231 Stephon231

2012/9/14

#
i need a reference for the world to get the countervalue from the Wombat class
danpost danpost

2012/9/14

#
Just put the code in the Wombat class.
Stephon231 Stephon231

2012/9/14

#
so would it be this: public void act() { if (leavesEaten == goal) { goal = 50 + 30; // Greenfoot.setWorld(new World2()); return; } }
danpost danpost

2012/9/14

#
Yes, but make sure if comes after the call to 'eatLeaves'.
Stephon231 Stephon231

2012/9/14

#
it says that it can't find variable goal
danpost danpost

2012/9/14

#
Add the class field 'goal' to the Wombat class
1
public static int goal = 50;
Should be placed like so
1
2
3
4
5
6
7
8
public class Wombat extends Actor
{
    public static int goal = 50;
     
    // constructors
 
    // methods
}
Stephon231 Stephon231

2012/9/14

#
when i get to 50 it changes to world2 like it is sopposed to but before it gets to seventy it restarts world2
There are more replies on the next page.
1
2
3