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?


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | 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); } } |
1 2 3 4 5 6 | if (getText().equals( "Xp3sTv57J" )) { Greenfoot.setWorld( new World2L0()); } else if (getText().equals( "KbZQ37x" )) { Greenfoot.setWorld( new World2L1()); } else if (getText().equals( "MPS5291" )) { // etc |