This site requires JavaScript, please enable it in your browser!
Greenfoot back
PascalFischer
PascalFischer wrote ...

2020/11/11

Playername

PascalFischer PascalFischer

2020/11/11

#
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
danpost danpost

2020/11/11

#
PascalFischer wrote...
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
The simple way would be to use Greenfoot.ask(String) to get the name and the UserInfo class to save/recall the name. You could put the asking in a for loop to limit attempts to be of proper length and either truncate or use a default name if all attempts fail to be of use.
PascalFischer PascalFischer

2020/11/13

#
danpost wrote...
PascalFischer wrote...
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
The simple way would be to use Greenfoot.ask(String) to get the name and the UserInfo class to save/recall the name. You could put the asking in a for loop to limit attempts to be of proper length and either truncate or use a default name if all attempts fail to be of use.
can you give me a Code example?
danpost danpost

2020/11/13

#
PascalFischer wrote...
can you give me a Code example?
This will get a name:
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;
    }
}
PascalFischer PascalFischer

2020/11/13

#
and how can I show this name over my Main Actor?
danpost danpost

2020/11/13

#
PascalFischer wrote...
and how can I show this name over my Main Actor?
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);
    }
}
Create player at end of started method in World subclass with: new Player(name) and add it to the world.
You need to login to post a reply.