How can i add a text to a world, which changes when i press a specific button (for example the space button)?
Example:
Text shown: Hello World
(I press space)
Text shown: Goodby World
if(Greenfoot.isKeyDown("space")) getWorld().showText("Goodbye World", 100, 100); else getWorld().showText("Hello World", 100, 100); //put any number you want instead of 100
if(Greenfoot.isKeyDown("space")) showText("Goodbye World", 100, 100); else showText("Hello World", 100, 100); //put any number you want instead of 100
//outside of a method, just in your class boolean hasPressedSpace = false; //then, in a method if(Greenfoot.isKeyDown("space")) hasPressedSpace = true; if(hasPressedSpace) getWorld().showText("Goodbye World", 100, 100); else getWorld().showText("Hello World", 100, 100);
// outside String[] greeting = { "Hi", "Hello" }; // in world constructor showText(greeting[Greenfoot.getRandomNumber(2)]+" World", 100, 100); // in act if ("space".equals(Greenfoot.getKey()) showText("Goodbye World", 100, 100);
// outside String[] salutation = { "Hi", "Hello", "Goodbye" }; // in constructor salute(Greenfoot.getRandomNumber(2)); // in act if ("space".equals(Greenfoot.getKey()) salute(2); // added method private void salute(int index) { showText(salutation[index]+" World", 100, 100); }