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

2017/5/10

How do I record what the user types?

CodeBoy CodeBoy

2017/5/10

#
I am making a typing game however my current code is not working. I need it to record what the user is typing and store it in a variable that I can compare to the word they were supposed to type. Can anyone tell me how I can do this? My current code is this:
public class Garden extends World
{
    
    private String Typed;
    private int timer = 60;
    
    /**
     * Constructor for objects of class Garden.
     * 
     */
    public Garden()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        
        Score.setScore(0);
        Score.addToWorld(this, 800, 20);
        
        Lives.setLives(3);
        Lives.addToWorld(this, 800, 40);
        
        WordBank1.addToWorld(this, 340, 200 );
    }
    
    public void CountDown()
    {
        if (timer>0)
        {
            timer--;
            if(timer == 0)
            {
                Lives.minus(1);
                timer = 60;
            }
        }
    }
    
    private void DetectType()
    {
        if (Greenfoot.isKeyDown("a")) {
            Typed = Typed + "a";
        }
        if (Greenfoot.isKeyDown("b")) {
            Typed = Typed + "b";
        }
        if (Greenfoot.isKeyDown("c")) {
            Typed = Typed + "c";
        }
        if (Greenfoot.isKeyDown("D")) {
            Typed = Typed + "d";
        }
        if (Greenfoot.isKeyDown("e")) {
            Typed = Typed + "e";
        }
        if (Greenfoot.isKeyDown("f")) {
            Typed = Typed + "f";
        }
        if (Greenfoot.isKeyDown("g")) {
            Typed = Typed + "g";
        }
        if (Greenfoot.isKeyDown("h")) {
            Typed = Typed + "h";
        }
        if (Greenfoot.isKeyDown("i")) {
            Typed = Typed + "i";
        }
        if (Greenfoot.isKeyDown("j")) {
            Typed = Typed + "j";
        }
        if (Greenfoot.isKeyDown("k")) {
            Typed = Typed + "k";
        }
        if (Greenfoot.isKeyDown("l")) {
            Typed = Typed + "l";
        }
        if (Greenfoot.isKeyDown("m")) {
            Typed = Typed + "m";
        }
        if (Greenfoot.isKeyDown("n")) {
            Typed = Typed + "n";
        }
        if (Greenfoot.isKeyDown("o")) {
            Typed = Typed + "o";
        }
        if (Greenfoot.isKeyDown("p")) {
            Typed = Typed + "p";
        }
        if (Greenfoot.isKeyDown("q")) {
            Typed = Typed + "q";
        }
        if (Greenfoot.isKeyDown("r")) {
            Typed = Typed + "r";
        }
        if (Greenfoot.isKeyDown("s")) {
            Typed = Typed + "s";
        }
        if (Greenfoot.isKeyDown("t")) {
            Typed = Typed + "t";
        }
        if (Greenfoot.isKeyDown("u")) {
            Typed = Typed + "u";
        }
        if (Greenfoot.isKeyDown("v")) {
            Typed = Typed + "v";
        }
        if (Greenfoot.isKeyDown("w")) {
            Typed = Typed + "w";
        }
        if (Greenfoot.isKeyDown("x")) {
            Typed = Typed + "x";
        }
        if (Greenfoot.isKeyDown("y")) {
            Typed = Typed + "y";
        }
        if (Greenfoot.isKeyDown("z")) {
            Typed = Typed + "z";
        }
        if (Greenfoot.isKeyDown("enter")) {
            if (Typed == WordBank1.Word)
            {
                Score.add(1);
            }
            else
            {
                Lives.minus(1);
            }
        }
    }
}
danpost danpost

2017/5/10

#
Your use of 'isKeyDown' is problematic. It is virtually impossible, in a scenario running at normal speed, to enter a key and not have it register as being down for multiple consecutive act steps. The method is excellent for gameplay; but, for text input, better is to use the Greenfoot class method 'getKey'. Another thing I should mention is the way you are comparing 'Typed' with 'WordBank1.Word'. You are asking if the two objects are one in the same. The contents (or character seequencee) may be equivalent, but the objects that contain them will not be one in the same. To compare the character sequences of two different String objects, use the 'equals' method:
if (WordBank1.Word.equals(Typed))
You may want to also allow the user to correct a mistake when keying in the word by implementing "backspace" control.
CodeBoy CodeBoy

2017/5/10

#
I tried converting the isKeyDown to getKey but I get the error "method getKey in class greenfoot.Greenfoot cannot be applied to given types;"
private void DetectType()
    {
        if (Greenfoot.getKey("a")) {
            Typed = Typed + "a";
        }
        if (Greenfoot.getKey("b")) {
            Typed = Typed + "b";
        }
        if (Greenfoot.getKey("c")) {
            Typed = Typed + "c";
        }
        if (Greenfoot.getKey("D")) {
            Typed = Typed + "d";
        }
        if (Greenfoot.getKey("e")) {
            Typed = Typed + "e";
        }
        if (Greenfoot.getKey("f")) {
            Typed = Typed + "f";
        }
        if (Greenfoot.getKey("g")) {
            Typed = Typed + "g";
        }
        if (Greenfoot.getKey("h")) {
            Typed = Typed + "h";
        }
        if (Greenfoot.getKey("i")) {
            Typed = Typed + "i";
        }
        if (Greenfoot.getKey("j")) {
            Typed = Typed + "j";
        }
        if (Greenfoot.getKey("k")) {
            Typed = Typed + "k";
        }
        if (Greenfoot.getKey("l")) {
            Typed = Typed + "l";
        }
        if (Greenfoot.getKey("m")) {
            Typed = Typed + "m";
        }
        if (Greenfoot.getKey("n")) {
            Typed = Typed + "n";
        }
        if (Greenfoot.getKey("o")) {
            Typed = Typed + "o";
        }
        if (Greenfoot.getKey("p")) {
            Typed = Typed + "p";
        }
        if (Greenfoot.getKey("q")) {
            Typed = Typed + "q";
        }
        if (Greenfoot.getKey("r")) {
            Typed = Typed + "r";
        }
        if (Greenfoot.getKey("s")) {
            Typed = Typed + "s";
        }
        if (Greenfoot.getKey("t")) {
            Typed = Typed + "t";
        }
        if (Greenfoot.getKey("u")) {
            Typed = Typed + "u";
        }
        if (Greenfoot.getKey("v")) {
            Typed = Typed + "v";
        }
        if (Greenfoot.getKey("w")) {
            Typed = Typed + "w";
        }
        if (Greenfoot.getKey("x")) {
            Typed = Typed + "x";
        }
        if (Greenfoot.getKey("y")) {
            Typed = Typed + "y";
        }
        if (Greenfoot.getKey("z")) {
            Typed = Typed + "z";
        }
        if (Greenfoot.getKey("enter")) {
            if (WordBank1.Word.equals(Typed))
            {
                Score.add(1);
            }
            else
            {
                Lives.minus(1);
            }
        }
    }
danpost danpost

2017/5/10

#
Did you look at the Greenfoot class API documentation for information on the 'getKey' method before trying to use it? The 'getKey' method, first off, requires NO parameters and returns either 'null' or the value of a key that has been entered. You do not specify a key for it. You should end up with something like this:
private void DetectType()
{
    String key = Greenfoot.getKey();
    if (key == null) return;
    if ("backspace".equals(key))
    {
        return; // replace with appropriate code if desired
    }
    if ("enter".equals(key))
    {
        if (WordBank1.Word.equals(Typed))
        {
            Score.add(1);
            WordBank1.setWord(); // whatever you do to set a new word in Word of WordBank1
        }
        else
        {
            Lives.minus(1);
            Typed = "";
        }
        return;
    }        
    key = key.toLowercase();
    if (key.length() == 1 && key.charAt(0) >= 'a' &&  key.charAt(0) <= 'z')
    {
        Typed += key;
    }
}
CodeBoy CodeBoy

2017/5/10

#
Sorry I'm new to greenfoot and still have a lot to learn. I've updated my code but when I run the game it doesn't do anything.
private void DetectType()
    {
        String key = Greenfoot.getKey();
       if (key == null) return;
        if ("backspace".equals(key))
        {
        return; // replace with appropriate code if desired
       }
       if ("enter".equals(key))
          {
           if (WordBank1.Word.equals(Typed))
           {
            Score.add(1);
            WordBank1.newWord(); // whatever you do to set a new word in Word of WordBank1
           }
           else
           {
            Lives.minus(1);
            Typed = "";
           }
           return;
          }        
          key = key.toLowerCase();
          if (key.length() == 1 && key.charAt(0) >= 'a' &&  key.charAt(0) <= 'z')
          {
              Typed += key;
       }
    }
danpost danpost

2017/5/10

#
Do you have 'DetectType' (or 'CountDown') being called from anywhere? (or, more precisely, do you have 'DetectType();' anywhere in your codes? No. From what I can tell from what you gave previously, it appears you are not calling the method from anywhere. Add the following method to your world class code:
public void act()
{
    DetectType();
}
CodeBoy CodeBoy

2017/5/10

#
Thank you I've changed the code and it is now working.
You need to login to post a reply.