Hello, I have a part of my game that allows you to type in your name. After you type your name and press enter, I want the actor to stop all of the code in it's class. Then after that, the game starts and you move other actors using the key board.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.util.Scanner;
/**
* Write a description of class P1Input here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class P1Input extends Input
{
static final int MAX_INPUT_LENGTH = 20;
String text = "P1 Name";
boolean used;
boolean on;
public static final Color transparent = new Color(0,0,0,0);
public P1Input()
{
updateImage();
text = "";
on = true;
}
private void updateImage()
{
GreenfootImage image = new GreenfootImage(15*MAX_INPUT_LENGTH, 30);
image.setColor(new Color(128, 0, 0));
image.fill();
image.setColor(Color.lightGray);
image.fillRect(3, 3, image.getWidth()-6, 24);
GreenfootImage textImage = new GreenfootImage(text, 24, Color.black, null);
image.drawImage(textImage, (image.getWidth()-textImage.getWidth())/2, 15-textImage.getHeight()/2);
setImage(image);
}
public void act()
{
changeImage();
used = false;
int i = 1;
if (!used && i == 1)
{
String key = Greenfoot.getKey();
if (key == null)
{
return;
}
if ("backspace".equals(key) && text.length() > 0)
{
text = text.substring(0, text.length() - 1);
}
if ("escape".equals(key))
{
text = "";
}
if ("space".equals(key))
{
key = " ";
}
if (key.length() == 1 && text.length() < MAX_INPUT_LENGTH) text += key;
updateImage();
}
if (Greenfoot.isKeyDown("enter") )
{
used = true;
P2Input p2Input = new P2Input();
setImage(new GreenfootImage(getText() + "'s Lives:", 30, Color.black, transparent));
if(on)
{
on = false;
getWorld().addObject(p2Input, 750, 25);
}
}
}
public String getText()
{
return text;
}
}
