i cannot figure out what is wrong with my image!
Here is the code:
and here is the problem- When i press the backspace key, it successfully works- but not until you type again.
The image is constantly set, what is the problem?
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 | public void act() { String myKey = Greenfoot.getKey(); if (myKey == null ) { return ; } String myText = "" ; if (myKey == "space" ) { myKey = " " ; } // If spaces are not wanted, change 'myKey = " ";' to 'return;' if (myKey == "backspace" ) { // Code for when 'backspace' is pressed if (txtString.length() > 0 ) { txtString = txtString.substring( 0 , txtString.length() - 1 ); } return ; } if (myKey == "enter" ) { // Code for when 'enter' is pressed myKey = "\n" ; } // You can continue for other keys: arrow keys, function keys, 'tab' and 'escape' (and any others I may have missed) // Change the concatenation in the following statement to only wanted characters String goodChars = LETTERS + letters + Numbers + Symbols + " " + "\n" ; int myIndex = goodChars.indexOf(myKey.charAt( 0 )); if (myIndex > - 1 ) { myText = goodChars.substring(myIndex, myIndex + 1 ); txtString = txtString + myText; } GreenfootImage image = new GreenfootImage(txtString, 15 , Color.BLACK, Color.WHITE); setImage(image); } |