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:
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:
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:
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 ^^
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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 ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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()); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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 ; } ... |