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);
}
}
