Hi,
i have a problem. I tried to add an text field wich checks the tiped code if click at the screen. And opens a World wich fits with that typed code. But my programm opens one world every time? How can i do it?
import greenfoot.*;
import java.awt.Color;
/**
* An actor for text input in Greenfoot.
*
* @author Sven van Nigtevecht
* @version 1.0
*/
public class TextField extends Actor
{
private GreenfootImage beginImage;
private String text;
/**
* Create a new text field with a width of 400.
*/
public TextField()
{
this(400);
}
/**
* Create a new text field with a given width.
*/
public TextField(int width)
{
GreenfootImage img = new GreenfootImage(width, 50);
img.setColor(Color.WHITE);
img.fill();
img.setColor(Color.WHITE);
img.fillRect(3,3, img.getWidth() -6, img.getHeight() -6);
img.setTransparency(175);
setImage(img);
this.beginImage = img;
this.text = "";
}
/**
* Sets the prefix of the text field (the text you see before
* you typed anything).
*/
/**
* Sets the responder of the text field.
*/
/**
* Returns the responder of the text field.
*/
/**
* Returns the text of the text field.
*/
public String getText()
{
if (this.text != null) return this.text;
return "";
}
/**
* Returns the image of the text field, without the text.
*/
public GreenfootImage getBeginImage()
{
return this.beginImage;
}
/**
* Add text onto the text field if needed.
*/
public void act()
{
addInput();
if(Greenfoot.isKeyDown("n")){
if(getText() == "Xp3sTv57J"){
Greenfoot.setWorld(new World2L0());
}else{
if(getText() == "KbZQ37x"){
Greenfoot.setWorld(new World2L1());
}else{
if(getText() == "MPS5291"){
Greenfoot.setWorld(new World2L2());
}else{
if(getText() == "JsX31pQr"){
Greenfoot.setWorld(new World2L3());
}else{
if(getText() == "IIMJPZx1"){
Greenfoot.setWorld(new World3L0());
}else{
if(getText() == "KKJxMpQr2"){
Greenfoot.setWorld(new World3L1());
}else{
if(getText() == "15K3P9XwA"){
Greenfoot.setWorld(new World3L2());
}else{
if(getText() == "B3uVTsM5"){
Greenfoot.setWorld(new World3L3());
}else{
Greenfoot.setWorld(new World1());
}
}
}
}
}
}
}
}
}
}
private void addInput()
{
String key = Greenfoot.getKey();
if (key == null) return;
if (key.equals("backspace")){
if (text.length() > 0) {
text = text.substring(0, text.length() -1);
}
else {
if(key.equals("space")){
key = " ";
}
if (key.equals("shift") ||
key.equals("control") ||
key.equals("tab") ||
key.equals("escape") ||
(char)127 == key.charAt(0)) {
key = "";
}
text = text + key;
}
}
refresh();
}
/**
* Redraw the image of the text field.
*/
public void refresh()
{
GreenfootImage img = new GreenfootImage(beginImage);
GreenfootImage txtImg = new GreenfootImage(this.text, 40, null, null);
int x = 10;
int y = img.getHeight() /2 - txtImg.getHeight() /2;
img.drawImage(txtImg, x,y);
setImage(img);
}
}
if(getText().equals("Xp3sTv57J")) {
Greenfoot.setWorld(new World2L0());
} else if(getText().equals("KbZQ37x")) {
Greenfoot.setWorld(new World2L1());
} else if(getText().equals("MPS5291")) {
// etc