I am trying to have the player walk up to a certain point, press enter, and a text box will appear. I know how to do the text class and how to display the text


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 | public class TextWorld extends World { /** * Constructor for objects of class MyWorld. * */ public TextWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 700 , 500 , 1 ); // Resizing background picture to fit screen GreenfootImage Bg = new GreenfootImage ( "pokemon.png" ); //image name and file type. the image must be in the project folder created when started scenario Bg.scale (getWidth (), getHeight ()); // Can take anyway these 3 lines, if don't want any image/background setBackground (Bg); // Creating and adding the text (an object) to the world addObject ( new TextForTest ( "Pokemon" ), 286 , 80 ); //first number L/R: greater moves it to the right. Second Up or down: greater = down // Creating and adding the button PlayButton button = new PlayButton (); addObject (button, 500 , 400 ); } } |
1 2 3 | TextForTest textActor = new TextForTest ( "Pokemon" ); TriggerText trigger = new TriggerText(textActor); addObject(trigger, 286 , 80 ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import greenfoot.*; public class TriggerText extends Actor { Actor toShowl; public TriggerText(Actor actor) { toShow = actor; // set image here or set a default image for the classs } public void act() { if ( toShow == null ) return ; // no actor to show if ( toShow.getWorld() != null ) return ; // actor already in world if ( ! isTouching(Player. class ) ) return ; // not touching player if ( ! Greenfoot.isKeyDown( "enter" ) ) return ; // 'enter' key not pressed getWorld().addObject(toShow, getX(), getY()); getWorld().removeObject( this ); // optional } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public class TriggerText extends Actor { Actor toShow; public TriggerText (Actor actor) { toShow = actor; GreenfootImage Bg = new GreenfootImage ( "pokemon.png" ); //image name and file type. the image must be in the project folder created when started scenario Bg.scale (getWidth (), getHeight ()); // Can take anyway these 3 lines, if don't want any image/background setBackground (Bg); } /** * Act - do whatever the TriggerText wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // (toShow.getWorld () !=null ) return; if (! Greenfoot.isKeyDown ( "enter" ) ) return ; getWorld ().addObject (toShow, getX (), getY ()); } } |