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

2019/4/8

user input storage

badboydown badboydown

2019/4/8

#
I need help with storing the string value (player name) to be able to pass it onto another world (to display it as the player's name while the game is ongoing).
public String forInput()
    {
       String name = "";
       key= Greenfoot.getKey();

        if (Arrays.asList(alphabet).contains(key) && input.length() == 1)
        {
            if (playername == null)
            {
                playername = "";
            }
                playername += input;        
                displayText(playername, x, y, 30); 
                
                name = playername;
                
        }
        return name;
    }
This is where it gets tricky, i cant seem to pass the updated value of playername to String name
Super_Hippo Super_Hippo

2019/4/8

#
What is "input"? Shouldn't it be "key"? And what is (or should be) the difference between "name" and "playername" (or why two variables for the same thing)?
danpost danpost

2019/4/8

#
The method will only ever pick up just one character of input. Also, that character will never be the exact same as one in your array (similar maybe, but not the same). To explain better, take the word 'add'. It has two 'd's in it. The first 'd' is not the second 'd'. Similarly, the input character would be a separate entity from any character in your array, You might want to consider using Greenfoot.ask, in this case.
badboydown badboydown

2019/4/8

#
I actually just found a simpler code for this,
public String forInput()
    {
        input = Greenfoot.getKey();

       
        if ("space".equals(input))
        {
            playername += " ";
            displayText(playername, x, y, 30);
        }

        else if ("backspace".equals(input))
        {
            int size = playername.length();
            if (size > 0)
            {
                playername = playername.substring(0, size - 1);
                displayText(playername, x, y, 30);
            }
        }
        else if (input != null)
        {
            playername += input;
            displayText(playername, x, y, 30);
        }

        return playername;
    }
badboydown badboydown

2019/4/8

#
I tried to use Greenfoot.ask() but i would prefer entering data without the prompt window popping up
danpost danpost

2019/4/9

#
You are still missing some important things -- like (1) how to determine when the name has been completely entered and (2) allowing only 1-character string input to be concatenated to the name. Also, there is no apparent visual clues as to where the input is taking place at (or which name is being entered). Is this something the world is dealing with somehow?
You need to login to post a reply.