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

2016/4/27

Problem in implementing Starting Menu

Nagogi Nagogi

2016/4/27

#
Recently, I had problems when I create some kind of menu on my project. So, I have a sub class and a super class (that extends sub class) from the world. In my sub class there's a prepare() method that adds object into my world (super class) and in my super class there's a constructor that creates the starting menu (that refers from menu actor). Here's my code on my super class:
public class Level01 extends Level
{
    public Level01()
    {
        if(menu)
        {
            Menu m=new Menu();
            addObject(m, getWidth()/2, getHeight()/2);
            if(m.getIsDone())
            {
                menu=false;
                super.prepare();
            }

        }
    }
}
and here's from the sub class :
public abstract class Level extends World
{
    public void prepare()
    {
        showText("Nyawamu", 600, 20);
        addObject(new Hatiku(),530,30);
        addObject(score, 700, 40);
        addObject(healthbar, 600, 40);
    }
}
My problem is that when I try run it, my expectation is that all of the objects in my prepare() method from the sub class will appear after I click the 'start game' button on my menu and the menu dissapears. But it doesn't appear like the way I want to. can you help me?
danpost danpost

2016/4/27

#
The main problem is where you placed the lines 9 through 13 in the Level101 class. The block of code, lines 3 through 16 is called the constructor of the Level101 class. It is executed one time per object created from the class -- or, once, when your Level101 world is created. The menu will certainly not be in the state of "is done" at the time it is being adding it into the world -- and since the code (lines 3 through 16) will NEVER execute again for that particular Level101 world object, lines 11 and 12 will never be executed. The lines 9 through 13 need to be somewhere where the code will repeatedly be ran until such time as the condition "m.getIsDone()" becomes true. This means making use of the 'act' method which greenfoot will call repeatedly as long as that world is the active world. This will now create another wrinkle. The variable 'm' declared on line 7 only lasts until the constructor completes execution. How can it be referenced in the 'act' method? Move line 7, the line that declare 'm' as a Menu reference variable and assigns it a new Menu object, to before line 3. Now 'm' will continue to exist for as long as the Level101 world exists.
You need to login to post a reply.