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

2021/1/15

need help regarding main menu

Zurah Zurah

2021/1/15

#
since my game is nearing completion now, i wanted to implement a sort of "main menu" where some basic info is displayed, along with a button that, when pressed, teleports you into the actual game world. for this, i set up a second world called "Hauptmenü" (english: main menu). The content of that class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Hauptmenü here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Hauptmenü extends World
{
    
    /**
     * Constructor for objects of class Hauptmenü.
     * 
     */
    public Hauptmenü()
    {    
        super(1620, 900, 1); 
        addObject(new Startbutton(), 1550, 830);
    }
}
basically, it just sets the image up to scale properly with the game, and places a start button in the bottom right. the code of the start button:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Startbutton here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Startbutton extends Menu
{
    /**
     * Act - do whatever the Startbutton 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))
            {
                Greenfoot.setWorld(new MyWorld());
            }
    }    
}
with this, i wanted to achieve that when pressed, as i said, it will teleport the player into the actual gameworld called "MyWorld" per default. since i had issues making the main menu display before the actual world displays, i set something up in my MyWorld.class to make the main menu show up first. The contents of that:
import greenfoot.*;
 
public class MyWorld extends World
{
    static final int WIDE = 27, HIGH = 15, CELL = 60;
    static final int BIG_X = 100, BIG_Y = 50;
     
    int[][] world = new int[BIG_Y][BIG_X];
    int scrollX, scrollY;
    Actor player = new Spieler();
    
    
     
public MyWorld()
    {
        super(WIDE, HIGH, CELL);
        setPaintOrder(Spieler.class, Boden.class);
        setup();
           
        addObject(player, WIDE/2, HIGH/2);
}
     

private void setup()
    {
        int menu = 0;
        if(menu == 0)   {
            Greenfoot.setWorld(new Hauptmenü());
            menu = 1;
        }
...       
the idea was that the int "menu" is set to 1 after opening the main menu, so thhat when i close it by clicking on the start button, it wont open the menu again since its on 1, meaning that the Greenfoot.setWorld function wont run I realized though that since i set up a "Greenfoot.setWorld(new MyWorld());" in my start button class, the entire MyWorld class is run through again, creating an endless cycle that results in the main menu closing when u click the start button, then displaying the game world for about a frame and then opening the main menu again. I am struggling to find a workaround for this, so if any of you have an idea, i would appreciate the help ^^
danpost danpost

2021/1/15

#
Remove code shown in setup method above and manually create a main menu world (right click on class and select "new Hauptmenü()").
Zurah Zurah

2021/1/15

#
that actually results in the same problem of the main menu opening itself again lol
danpost danpost

2021/1/15

#
Zurah wrote...
that actually results in the same problem of the main menu opening itself again lol
Did you remove lines 26 thru 30 above?
Zurah Zurah

2021/1/15

#
yup
Zurah Zurah

2021/1/15

#
problem solved; i had the exact snippet of code in my player class aswell for some random reason, everything works fine now ^^
danpost danpost

2021/1/15

#
Zurah wrote...
automate the part of manually clicking
You know how ridiculous that sounds? You do not automate something that you are supposed to do manually. You just do it manually once and your project will start with that world (even after resetting or re-compiling).
RcCookie RcCookie

2021/1/15

#
Where did he write that quote?
danpost danpost

2021/1/15

#
RcCookie wrote...
Where did he write that quote?
It must have been edited out before I posted.
You need to login to post a reply.