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

2013/9/29

Start a "set"-method | Start the scenario

Tiakuran Tiakuran

2013/9/29

#
Hi there, community (: I'm quite a new user of Greenfoot, that's just because we have to do it in school. Actually, I really like programming and I'm working for hours on an own game. The content of the game doesn't matter. Maybe it's just stupid, but I have a small question about some points. There are the following: 1 - I currently work on a multiplayer game. I want the game to ask (maybe in a pop-up) for the number of players before the game starts and depending on that int-number adds one, two or whatever objects in the world. I have the method for setting a number and the different types of options, but I can't fix something like a pop-up, which asks me DIRECTLY (before starting the whole game) for the number. I always have to right-click in the world and set the number manually.. I didn't found some information how can I start a "set"-Method automatically after resetting the world. 2 - Then I also have the problem with starting the scenario. I looked in the library and found a "Greenfoot.start();" method, but unfortunately this method doesn't work if I put it into the act-method of my first world. Do I have to put the start-method somewhere else or how can I make my scenario to start automatically AFTER I set the int number (point 1). Sorry for my bad English... I speak German, but quite every discussion here is in English ^^"
erdelf erdelf

2013/9/29

#
u should put both in the constructor method
greenfoot.start();
belongs to the end of the constructor.
int playerCount = (int)JOptionPane.showInputDialog(
                    frame,
                    "How many players are playing?",
                    "Customized Dialog",
                    JOptionPane.PLAIN_MESSAGE,
                    icon,
                    possibilities,
                    "1");
this should do what u want in 1.), just write it in the constructor (untested)
Busch2207 Busch2207

2013/9/29

#
The code, erdelf wrote, will not work. JOptionPane.showInputDialog (...) returns a string that can not be converted to an int value so easily... In addition, some variables, such as frame, icon or possibilities are unknown... I quickly wrote a method for you, that opens a dialog box again and again until you entered a number greater than 0. I have commented on it quite extensively. ;)
    public int getIntInput(String str_Question,String str_Name)
    {
        while(true) // Solange keine Nummer eingegeben wurde, wird diese while-Schleife immer wieder wiederholt und das Dialogfeld immer wieder aufgerufen
        {
            String str_Input = JOptionPane.showInputDialog( // lässt einen Dialog zeigen, der einen String zurückgibt
                    null,
                    str_Question, // Was im Dialog stehen soll
                    str_Name, //Name des Dialogs
                    JOptionPane.PLAIN_MESSAGE); // Öffnet einen Input-Dialog
            if(str_Input==null) // überprüft, ob eine Eingabe gemacht wurde
                continue; // wenn keine Eingabe erfolgt ist springt der Compiler zurück zur letzten while- oder for- Bedingung. In diesem Fall wird also ein neues Dialog-Feld erzeugt
            str_Input=str_Input.replaceAll("[^0-9]", ""); // Löscht alle Symbole aus dem String, die keine Ziffern sind
            try
            {
                int i_Number=Integer.parseInt(str_Input); // erzeugt eine Zahl aus dem String
                if(i_Number>0) // überprüft, ob die eingegebene Zahl größer als 0 ist
                    return i_Number; // gibt die Zahl zurück und bricht die Methode ab
            }
            catch(Exception e) // wenn in dem String keine Zahl vorhanden ist oder noch andere Symbole außer Ziffern enthalten sind (was bei diesem String nicht mehr möglich ist), wird hierher gesprungen und der Code in den kommenden geschweiften Klammern ausgeführt
            {
            }
        }
    }
You can easily use this method by:
int i_PlayerNumber=getIntInput("How many players are playing?","Customized Dialog");
You only have to import the JOptionPane-class, that she can be used in the code. To do this, just write the following code in one of the first lines in the source Code
import javax.swing.JOptionPane;
I hope, it helps! :)
danpost danpost

2013/9/29

#
You could create a second world class and have the first one set it active when the scenario is started. Pass 'this' to the new world so we can return to it. This second world is to save the main world in an instance field and display options (buttons to click on or a message asking the user to press one or two for the number of players). When the action is complete, call the set method in the main world to pass the value and set the active world back to the main one, both actions using the world saved in the instance field.
Tiakuran Tiakuran

2013/9/29

#
Wow! Busch, your code was perfect, with all the comments I really understood what you did there for me. Didn't expect such a fast and great help here. Thank you :) Now the game works perfect. The only problem is, that it only runs one time and after resetting the scenario the objects don't move if I press the keys. But doesn't matter, it works when you start Greenfoot! Again: thank you and have a nice evening ^^
You need to login to post a reply.