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

2018/4/28

Help with looping textbox?

StormP3 StormP3

2018/4/28

#
This is the code I am using, towards the bottom section, there is a spot where it mentions words with an input spot and such, this is the trouble spot I am having as I need a way to loop it to the other words at the top (private String words = {"cat", "dog", "love"}; etc . ) public class StringInputBox extends Actor { static final int MAX_INPUT_LENGTH = 20; String text = "Type Here"; String input = " "; private String words = { "cat", "dog", "love", "box", "banana", }; //These are examples and real answers for now, changing this later public void act() { String key = Greenfoot.getKey(); if (key == null) return; if ("enter".equals(key) && text.length() > 0) { System.out.println("You entered String "+text); input = text; text = ""; correctIncorrect(); updateImage(); } if ("backspace".equals(key) && text.length() > 0) text = text.substring(0, text.length() - 1); if ("escape".equals(key)) text = ""; if ("space".equals(key)) key = " "; if (key.length() == 1 && text.length() < MAX_INPUT_LENGTH) text += key; updateImage(); } public void correctIncorrect() { if (Greenfoot.isKeyDown("enter")) { if (words.equals(input)) { System.out.println("correct " + input); Greenfoot.setWorld(new Level2()); } else if (!words.equals(input)) { System.out.println("incorrect "+ input); } } } public StringInputBox() { updateImage(); text = ""; } private void updateImage() { GreenfootImage image = new GreenfootImage(15*MAX_INPUT_LENGTH, 30); image.setColor(new Color(128, 0, 0)); image.fill(); image.setColor(Color.cyan); image.fillRect(3, 3, image.getWidth()-6, 24); GreenfootImage textImage = new GreenfootImage(text, 24, Color.black, null); image.drawImage(textImage, (image.getWidth()-textImage.getWidth())/2, 15-textImage.getHeight()/2); setImage(image); } }
danpost danpost

2018/4/28

#
StormP3 wrote...
public void correctIncorrect()
{
    if (Greenfoot.isKeyDown("enter")) 
    {
        if (words[0].equals(input))
        {
            System.out.println("correct " + input);
            Greenfoot.setWorld(new Level2());
        }
        else if (!words[0].equals(input)) {
            System.out.println("incorrect "+ input);
        }
    }
}
The above it what you intended to show for what you had for the correctIncorrect method. Here, lines 5 and 10 have hard-coded zeroes for which element in the array to compare to. You want to be able to vary that value; so, add an int field to act as an index pointer to the array. Use its value to load and compare, plus you can use it to either stop the scenario or begin again at the first word after all have been given. Btw, the first if condition was already checked on before calling this method. It does not need to be checked again.
StormP3 StormP3

2018/4/28

#
I'm not very good at the coding thing, and I don't really know the coding pieces to it. Is the int field a 0-9 kind of thing? or am I getting this mixed up with something else?
StormP3 StormP3

2018/4/28

#
I was digging around and I think i kind of get what your talking about, but I just want to make sure. Do you mean to create an int value set that allows me to say multiple numbers as follows in the string? I've been looking at the different kinds and am a little overwhelmed with the differences between some and similarities between others so i don't know which to use. And is there a way to put multiple numbers in to one line?
if (words[0 || 1].equals(input))
//or
if (words[0,1,2].equals(input))
danpost danpost

2018/4/28

#
StormP3 wrote...
I'm not very good at the coding thing, and I don't really know the coding pieces to it. Is the int field a 0-9 kind of thing? or am I getting this mixed up with something else?
You are probably getting it mixed up with something else.
is there a way to put multiple numbers in to one line?
The int variable field would replace those zeroes so that you can change what element is indexed in the array. You could have a field like so:
private int wordIndex = 0;
and replace those line like so:
if (words[wordIndex].equals(input))
Then when changing words, do this:
wordIndex++;
Finally, you would follow that line with this:
if (wordIndex == words.length())
to detect all words have been used to either finish or re-start the words; otherwise do the newly indexed word.
StormP3 StormP3

2018/4/29

#
Where would I put the last two lines of code you suggested? The previous two helped a lot, and they allow the same thing to continue to run as before, but as I said previously, I'm not very good with int fields and changing things to be variables. Thanks for all your help so far!
StormP3 StormP3

2018/4/30

#
Never mind about that last post, I figured out where it goes and what to do after that, thanks for all your help!
You need to login to post a reply.