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