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

2021/11/1

How to keep my timer, and health from one world to another

jc.xi jc.xi

2021/11/1

#
Hello, I am new to this program and I have to have a bit of trouble. basically, I am creating a maze game, and the objective is to collect all the blue orbs before finishing the game. The thing is, once you collect an orb it's supposed to take you to a question(world), and depending on how you answer that question it will either lose a bit of life or it stays the same if answered correctly. I have it all done but when the question is answered and it goes back to the maze, the whole maze resets(the timer and health reset). any idea what I should do? I can provide code if need be.
danpost danpost

2021/11/1

#
You must return to the same world. Using the new keyword will create a new object. If you have worlds called MazeWorld and QuestionWorld, in MazeWorld, you would use:
Greenfoot.setWorld(new QuestionWorld(this));
In QuestionWorld, have:
World mazeWorld;

public QuestionWorld(World world)
{
    super(600, 400, 1);
    mazeWorld = world;
    ...
}
...
// with the following line somewhere
Greenfoot.setWorld(mazeWorld);
jc.xi jc.xi

2021/11/1

#
OK ok with that being said I have a two-part question: 1: with the line of code you provided "Greenfoot.setWorld(new QuestionWorld(this));", can you put it into a method in the maze class that returns a variable that stores that instances of the world? So that we can access it in other classes? 2:what if the ...new QuestionWorld () is being called in an Actor subclass? Because of that line that creates a new QuestionWorld we have something similar already but it's in the smooth mover subclass that I have created.
danpost danpost

2021/11/1

#
jc.xi wrote...
1: with the line of code you provided "Greenfoot.setWorld(new QuestionWorld(this));", can you put it into a method in the maze class that returns a variable that stores that instances of the world? So that we can access it in other classes?
Unnecessary. "this" IS that instance of the maze world when used in the MazeWorld class.
2:what if the ...new QuestionWorld () is being called in an Actor subclass? Because of that line that creates a new QuestionWorld we have something similar already but it's in the smooth mover subclass that I have created.
Then use:
Greenfoot.setWorld(new QuestionWorld(getWorld()));
You need to login to post a reply.