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

2014/12/8

I am trying to implement a 'change key' function

karolis karolis

2014/12/8

#
Im trying to implement a function for the user to be able to change a desired key using the storage.csv, im able to change, save and read the key. Everything is working as intended except that a key must be pressed before the function executes. Otherwise if the 'Greenfoot.getKey()' returns a null value my program stops working, as greenfoot does not like the function GetKeyPressed calling back on itself. Im not sure how to go about making this subclass work that it clears the Greenfoot.getKey(), so that the class AskKeyboardInput is able to ask the user for input, while GetKeyPressed waits for a keyboard input - basically just like the usual feature of being able to change a key.
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
32
33
34
35
36
37
38
39
40
41
42
43
44
{
    public KeyboardConfigThrust()
    {
        GreenfootImage image = new GreenfootImage(550, 50);
        image.setColor(Color.GREEN);
        float FontSize = 45.0f;
        Font font = image.getFont().deriveFont(FontSize);
        image.setFont(font);
         
        image.drawString("Thrust Forward", 1, 35);
        image.drawString(MainMenu.Thrust, 350, 35);
         
        setImage(image);
    }
    public void act()
    {
        if (Greenfoot.mouseClicked(this))
        {
            getWorld().addObject(new AskKeyboardInput(), MainMenu.resX/2, MainMenu.resY/2);
            //Greenfoot.getKey(); //clears getKey()
            GetKeyPressed();
        }
    }
    public void GetKeyPressed()
    {
        String key = Greenfoot.getKey();
        if (key != null)
        {
            MainMenu.Thrust = key;
             
            UserInfo PlayerInfo = UserInfo.getMyInfo();
            PlayerInfo.setString(0,MainMenu.Thrust);
            PlayerInfo.store();
            AskKeyboardInput.KeyInput = true;
            ResetImage();
        }
        else GetKeyPressed();
    }
    public void ResetImage()
    {
        getWorld().addObject(new KeyboardConfigThrust(), MainMenu.resX/3, (MainMenu.resY/16)*9);
        getWorld().removeObject(this);
    }
}
Thanks a lot Edit: Also, i am trying to do this to 6 keys, where as the storage.csv only allows for 5 string values, is there a way i could store the last key?
danpost danpost

2014/12/8

#
If you limit the key string length to one character then you will only need six characters of only one of the strings (leaving you with 244 more string characters to work with). Even if not, you can place a space beween each key string in just one of the strings (there is no way to reach 50 characters with only six different key names). You can use a while loop to wait for the key to come in. As long as there is time for the keystroke to be made, using Greenfoot.getKey() repeatedly is not a problem:
1
2
3
String key = null;
while ((key = Greenfoot.getKey()) == null || key.length() != 1) ;
// process key
karolis karolis

2014/12/10

#
Thanks again danpost However, my program becomes very unresponsive using this while function, even the AskKeyboardInput does not appear on the screen, even while using this. (I also put the 'ResetImage' inside the 'GetKeyPressed' as it was giving errors and its the only thing calling it anyway)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void GetKeyPressed()
{
    String key = null
    while ((key = Greenfoot.getKey()) == null)
    {
        if (key != null)
        {
            MainMenu.Thrust = key;
             
            UserInfo PlayerInfo = UserInfo.getMyInfo();
            PlayerInfo.setString(0,MainMenu.Thrust);
            PlayerInfo.store();
            AskKeyboardInput.KeyInput = true;
             
           getWorld().addObject(new KeyboardConfigThrust(), MainMenu.resX/3, (MainMenu.resY/16)*9);
           getWorld().removeObject(this);
        }
    }
}
i have also tried
1
2
3
4
5
String key = null
while ((key = Greenfoot.getKey()) == null || key.length() != 1)
{
    if ((key = Greenfoot.getKey()) != null || key.length() == 1)
    {
and
1
2
3
4
5
6
String key = Greenfoot.getKey();
while (key == null || key.length() != 1)
{
     
    if (key != null || key.length() == 1)
    {
karolis karolis

2014/12/10

#
how exactly does Greenfoot.getKey() work? ive read that after it is used it is then cleared to become null. is this true for when it is used in a decision too? e.g:
1
while ((key = Greenfoot.getKey()) == null || key.length() != 1)
karolis karolis

2014/12/10

#
For clarification, for the first code variation my program becomes unresponsive. For the second and third pieces of code, my program is responsive, but does not pick up any key inputs.
karolis karolis

2014/12/10

#
apologies for the previous rather rushed posts - i realised how they were rather silly, ive seem to have it working using this. however the AskKeyboardInput object still does not get created until after a key has been pressed, even though the GetKeyPressed is called after AskKeyboardInput
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public void GetKeyPressed() 
    String key = null;   
    while (key == null
    
       key = Greenfoot.getKey();
       if (key != null
       
           MainMenu.Thrust = key; 
               
           UserInfo PlayerInfo = UserInfo.getMyInfo();  
           PlayerInfo.setString(0,MainMenu.Thrust); 
           PlayerInfo.store(); 
           AskKeyboardInput.KeyInput = true
               
           getWorld().addObject(new KeyboardConfigThrust(), MainMenu.resX/3, (MainMenu.resY/16)*9); 
           getWorld().removeObject(this); 
        
    
}
danpost danpost

2014/12/10

#
I think you missed something in my last post. Line 2 (the while statement) has a semi-colon at the end of it. This says to run the loop without processing anything until a key of length one is entered through the keyboard. There is actually no code within the loop.
You need to login to post a reply.