I wonder if theres a Way to create a Field, were people can type a name they want (must have a lenght maximum), which is shown directly over the User, everytime he plays the game


1 2 3 4 5 6 7 8 9 10 11 | String name = "User" ; public void started() { for ( int i= 0 ; i< 3 ; i++) { String nm = Greenfoot.ask( "Enter name (no more than 10 characcters long)" ); if (nm == null || "" .equals(nm) || nm.length() > 10 ) continue ; name = nm; } } |
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 | String name; Actor namePlate; public Player(String nm) { name = nm; } protected void addedToWorld(World world) { namePlate = new NamePlate(); namePlate.setPosition(); } public void act() { // put at end and before any 'return;' statements namePlate.setPosition(); } // still in Player class private class NamePlate extends Actor { public NamePlate() { GreenfootImage img = new GreenfootImage(name, 17 , Color.BLACK, Color.CYAN); setImage(img); } public void setPosition() { if (Player. this .getWorld() == null ) { if (getWorld() != null ) getWorld().removeObject( this ); return ; } if (getWorld() == null ) Player. this .getWorld().addObject( this , 0 , 0 ); setLocation(Player. this .getX(), Player. this .getY()-Player. this .getImage().getHeight()/ 2 - 10 ); } } |