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

2014/9/2

Typing Game

Spock Spock

2014/9/2

#
Hi all. I'm currently trying to create a typing game where words appear on the screen and move towards the player. The player must then type and press enter to remove words from the screen. I am currently stuck trying to get the word typed to match a word displayed on the screen. To try and solve this i have the class which records what the player types (PType) call a method from a word on the screen. This method then determines if what the player types matches a word on the screen. I keep receiving an error, (java.lang.NullPointerException). THis occurs in the line "aword.checkWord(typing);" Here is code from a class which records what the player types: typing is the variable which holds the string the player has typed.
            if(k.equals("enter"))
            {
                
                SSGame ssgame = (SSGame) getWorld();
                List<AWord> AwList = ssgame.getObjects(AWord.class);
                for (AWord AW : AwList)
                {
                    
                    
                        AWord aword = ssgame.getAword();
                        aword.checkWord(typing); 
                    
                }
                //AWord aword = ssgame.getAword();
                //aword.checkWord(typing);
                typing = "";
            }
Here is code from the game world:
    private AWord aword;
    public AWord getAword()
    {
        return aword;
        
    }
Here is code from the word displayed on the screen:
    public void checkWord(String Check)
    {
        if (Check == ID)
        {
            SSGame ssgame = (SSGame) getWorld();
            ssgame.addObject(new Explosion(), getX(), getY());
            ssgame.removeObject(this);
        }
        else
        {
            //play incorrect sound
        }
    }
Super_Hippo Super_Hippo

2014/9/2

#
In line 6 and following (first code), you go through every AWord object which is currently in the world. But then, instead of using this 'AW', you get 'aword' from the world. Probably 'aword' is null, so you can't use a method with it. I think, what you want to do is, check every word on your screen if it is the same as the String which was typed. If any is correct, it will be removed, if the String isn't equal to any word on the screen, you should hear the incorrect sound. If you change line 10+11 to the following, it will call the 'checkWord' method in every object of the class 'AWord'.
AW.checkWord(typing);
The problem about your code is, that you will remove the correct word, but for every other word, you will hear the incorrect sound. What you could do is, have 'checkWord' return a boolean (whether or not the word was typed) and change the code above to
rightWorld = false;
//...
for (AWord AW : AwList)
{
    if (AW.checkWord(typing))
    {
        rightWord = true;
        break; // if you only have different words or only want to remove one at the time
    }
}

if (!rightWord) //play incorrect sound
typing = "";
Then the incorrect sound will only be played if the String isn't equal to any word in the world.
davmac davmac

2014/9/2

#
I keep receiving an error, (java.lang.NullPointerException). THis occurs in the line "aword.checkWord(typing);"
That implies that 'aword' is null at that point. Its value comes from the line above:
            AWord aword = ssgame.getAword();  
Looking at your game world code, I see you declare a variable - 'private AWord aword;' - to hold the word instance. But I don't see any code that actually sets it to anything, so it will be null by default. Perhaps that's the problem - you're never setting the variable to reference an actual word instance.
danpost danpost

2014/9/2

#
Line 3 of your 'checkWord' method is comparing two Strings using the conditional equality operator, '=='. This will not check to see if the character arrays of the strings are equal, but it will check to see if the memory addresses of the strings are the same (when objects are compared, the object reference pointers, or memory addresses, are compared to see if they are indeed the same object). To compare two different strings for similar character arrays, use the String class 'equals' method:
if (Check.equals(ID))
Spock Spock

2014/9/2

#
Thanks all for the help. Matching the word typed to a word on screen now works, however when i try to implement the code from Hippo for the sound, I get an incompatible types error. This occurs on line 9. Code form class recorder player keyboard:
            if(k.equals("enter"))
            {
                
                SSGame ssgame = (SSGame) getWorld();
                List<AWord> AwList = ssgame.getObjects(AWord.class);
                boolean rightword = false;
                for (AWord AW : AwList)
                {
                    if (AW.checkWord(typing))
                    {
                        AWord aword = ssgame.getAword();
                        AW.checkWord(typing); 
                        rightWord = true;
                    }
                }
                if (!rightword)
                {
                    //play incorrect sound;
                }
                typing = "";
            }
Code from word on screen:
    public void checkWord(String Check)
    {
        if (Check.equals(ID))
        {
            SSGame ssgame = (SSGame) getWorld();
            ssgame.addObject(new Explosion(), getX(), getY());
            ssgame.removeObject(this);
        }
        else
        {
            return false;
        }
    }
Spock Spock

2014/9/2

#
Also
danpost wrote...
Line 3 of your 'checkWord' method is comparing two Strings using the conditional equality operator, '=='. To compare two different strings for similar character arrays, use the String class 'equals' method:
if (Check.equals(ID))
Is there a way to compare the strings where the comparison is not case sensitive?
Super_Hippo Super_Hippo

2014/9/2

#
Change your method to this and try if it works then. To make it return a boolean, you have to replace the 'void' (which means it doesn't return anything) to 'boolean'.
    public boolean checkWord(String Check)
    {
        if (Check.equals(ID))
        {
            SSGame ssgame = (SSGame) getWorld();
            ssgame.addObject(new Explosion(), getX(), getY());
            ssgame.removeObject(this);
            return true;
        }
        else
        {
            return false;
        }
    }
To compare strings case insensitive, you can use 'equalsIgnoreCase' instead of 'equals'. Or you use 'toLowerCase()' on both strings and compare them then. The result is the same.
Spock Spock

2014/9/3

#
Thanks Hippo, that did the trick :)
You need to login to post a reply.