patternjavaMinor
TextField Validation for Teleport Feature in Game
Viewed 0 times
validationgameforteleporttextfieldfeature
Problem
I've implemented a teleport feature for my game and I would like some feedback on the way that I validate the user input. I am using the libGDX library.
When the player clicks the Teleport Menu button, a menu will pop up that has two TextFields pre-populated with zeros. The user can click the TextField to begin input, and then can input any string that they want to. Once the player clicks Teleport, the current Strings of the TextFields are retrieved and handed off to a method that turns them into valid integers.
Here is a screenshot of the Teleport Menu:
Here is the
```
public class TeleportMenu extends BZMenuTable {
private final NinePatch menuBackground;
private final MainGame game;
private final float stageWidth;
private final float stageHeight;
private TextField longitudeTextField;
private TextField latitudeTextField;
public TeleportMenu(Skin skin, Stage hudStage, NinePatch menuBackground, MainGame game, float stageWidth, float stageHeight) {
super(skin, hudStage);
this.menuBackground = menuBackground;
this.game = game;
this.stageWidth = stageWidth;
this.stageHeight = stageHeight;
}
@Override
public void build() {
TextButton teleportButton = new TextButton("Teleport", skin);
teleportButton.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
TeleportMenu.this.activateTeleport();
TeleportMenu.this.close();
}
});
this.menuTable.setBackground(new NinePatchDrawable(this.menuBackground));
this.menuTable.setPosition(this.stageWidth/10, this.stageHeight/8);
this.menuTable.setWidth((float)(this.stageWidth * 0.8));
this.menuTable.setHeight((float)(this.stageHeight * 0.6));
this.menuTable.add("Teleport");
this.menuTable.row();
this.menuTable.
When the player clicks the Teleport Menu button, a menu will pop up that has two TextFields pre-populated with zeros. The user can click the TextField to begin input, and then can input any string that they want to. Once the player clicks Teleport, the current Strings of the TextFields are retrieved and handed off to a method that turns them into valid integers.
Here is a screenshot of the Teleport Menu:
Here is the
TeleportMenu class, it is not too large:```
public class TeleportMenu extends BZMenuTable {
private final NinePatch menuBackground;
private final MainGame game;
private final float stageWidth;
private final float stageHeight;
private TextField longitudeTextField;
private TextField latitudeTextField;
public TeleportMenu(Skin skin, Stage hudStage, NinePatch menuBackground, MainGame game, float stageWidth, float stageHeight) {
super(skin, hudStage);
this.menuBackground = menuBackground;
this.game = game;
this.stageWidth = stageWidth;
this.stageHeight = stageHeight;
}
@Override
public void build() {
TextButton teleportButton = new TextButton("Teleport", skin);
teleportButton.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
TeleportMenu.this.activateTeleport();
TeleportMenu.this.close();
}
});
this.menuTable.setBackground(new NinePatchDrawable(this.menuBackground));
this.menuTable.setPosition(this.stageWidth/10, this.stageHeight/8);
this.menuTable.setWidth((float)(this.stageWidth * 0.8));
this.menuTable.setHeight((float)(this.stageHeight * 0.6));
this.menuTable.add("Teleport");
this.menuTable.row();
this.menuTable.
Solution
Table organization
I have mentioned before that I do not like this. This is not the "correct" way to setup a table on your screen. When you solve this issue, you will no longer need to pass around
Guess what the magic number is today?
Extract
processTextField
The first thing that comes to my mind is: If a user enters invalid input, should they then really be teleported?
If I enter the coordinate
That being said, I'd also make some other changes:
The both Math.max
However, there is a LibGDX method in MathUtils that already does this:
this.menuTable.setPosition(this.stageWidth/10, this.stageHeight/8);
this.menuTable.setWidth((float)(this.stageWidth * 0.8));
this.menuTable.setHeight((float)(this.stageHeight * 0.6));I have mentioned before that I do not like this. This is not the "correct" way to setup a table on your screen. When you solve this issue, you will no longer need to pass around
stageWidth and stageHeight.Guess what the magic number is today?
this.menuTable.add("Latitude 0 - 50");
this.menuTable.add("Longitude 0 - 50");Extract
50 to a static final int constant and use string concatenation to set the string:this.menuTable.add("Latitude 0 - " + MAX);processTextField
The first thing that comes to my mind is: If a user enters invalid input, should they then really be teleported?
If I enter the coordinate
-42, 1not2a3number4, I would expect an error message, not being teleported to (0, 0). Also remember that users will not be able to see System.out.println output.That being said, I'd also make some other changes:
- Use
intinstead ofIntegerforpositionvariable
- Instead of
if (position
- Instead of if (position >= MAX)
doposition = Math.min(MAX, position);
The both Math.max
and Math.min` uses can be put together into:position = Math.min(MAX, Math.max(0, position));However, there is a LibGDX method in MathUtils that already does this:
position = MathUtils.clamp(position, 0, MAX);Code Snippets
this.menuTable.setPosition(this.stageWidth/10, this.stageHeight/8);
this.menuTable.setWidth((float)(this.stageWidth * 0.8));
this.menuTable.setHeight((float)(this.stageHeight * 0.6));this.menuTable.add("Latitude 0 - 50");
this.menuTable.add("Longitude 0 - 50");this.menuTable.add("Latitude 0 - " + MAX);position = Math.min(MAX, Math.max(0, position));position = MathUtils.clamp(position, 0, MAX);Context
StackExchange Code Review Q#84278, answer score: 4
Revisions (0)
No revisions yet.