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

2015/4/12

new World with a Password?

jackandjim jackandjim

2015/4/12

#
We want that you can only play a level if you have a password which you get from the level before... Level code:
public Level2(String passwort)
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
            super(800, 800, 1); 
            PatAmer patamer = new PatAmer();
            addObject(patamer, 400, 400);
            patamer.setLocation(35, 400);
            //Lebenhintergrund
            Lebenshintergrund lebenshintergrund = new Lebenshintergrund();
            addObject(lebenshintergrund, 156, 61);
            lebenshintergrund.setLocation(122, 40);
            //Leben1
            Lebensanzeige1 lebensanzeige1 = new Lebensanzeige1();
            addObject(lebensanzeige1, 44, 40);
            //Leben2
            Lebensanzeige2 lebensanzeige2 = new Lebensanzeige2();
            addObject(lebensanzeige2, 119, 40);
            //Leben3
            Lebensanzeige3 lebensanzeige3 = new Lebensanzeige3();
            addObject(lebensanzeige3, 194, 40);
            if(passwort != "Glumanda"){
                Dunkelheit2 dunkelheit2 = new Dunkelheit2();
                addObject(dunkelheit2, 405, 407);
                dunkelheit2.setLocation(400, 400);
        }
    }
Now there is an error that we have to put a String inbetween the brackets but we want the person playing to type in the password...
Greenfoot.setWorld(new Level2());
I'd be happy to give any information if needed. Thank you in advance, J&J!
Super_Hippo Super_Hippo

2015/4/12

#
I would think it would be useful, if the 'setWorld' method would only be executed if the right password was typed in. So you may have a box in which you write the password. The entered string is saved as a 'private String text' for example. Then it could be something like this at:
private String password = "Glumanda";
private String text = "";

//in the method
String currentKey = Greenfoot.getKey();
//adjust the entered text if a key was pressed
//... try it out, if you have problems, report back
else if ("enter".equals(currentKey)
{
    if (text.equals(password)) Greenfoot.setWorld(new Level2());
    else
    {
        //reset entered text, maybe display an "invalid password" message
    }
}
As an alternative, you could also make use of the JOptionPanel class which can be used to create a pop up window in which you can input something, but in my opinion, it is more user-friendly to have it inside the world.
jackandjim jackandjim

2015/4/12

#
Alright, thanks a lot.
You need to login to post a reply.