-2

好的,所以我试图创建一个老式的 rpg 字符输入屏幕。我有一个上面有所有字母的图形,我想做的是当用户单击一个字母时,它将该字母设置为字符串的第一个值,如果他们单击另一个字母,它将设置它到字符串的第二个值,依此类推(最多 8 个字符)。单击退格键当然会删除最后输入的字母。

是否有捷径可寻?除了说,为所有 8 个字符输入的每个字符创建一个变量并将它们加在一起并检查每个字符

编辑 - 添加了我到目前为止的代码。我已将其从尝试使用名称的每个字符一个的一堆变量更改为使用 stringbuilder,但现在当我尝试删除最后一个字母时它会崩溃。

就测试而言,我只有点击 a 并点击退格检查

package states;

import org.lwjgl.input.Mouse;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import tools.FontFactory;
import tools.ResourceManager;

public class NameState extends BasicGameState {


private UnicodeFont font = null;

Image ename;
public String mouse = "No input yet!";

String namechar = "";
StringBuilder sb = new StringBuilder();

public int getID() {
    return Globals.STATE_NAME;
}

public void init(GameContainer gc, StateBasedGame game)
        throws SlickException {
    font = FontFactory.createFontL(ResourceManager.getFont("visitor")
            .getFontFile(), java.awt.Color.WHITE);

    ename = new Image("resources/graphics/entername.png");

}

public void render(GameContainer gc, StateBasedGame game, Graphics g)
        throws SlickException {
    g.setFont(font);
    ename.draw(0, 0);
    g.drawString(namechar + sb.toString(), 130, 40);
}

@Override
public void update(GameContainer container, StateBasedGame game, int delta)
        throws SlickException {

    int xpos = Mouse.getX();
    int ypos = Mouse.getY();

    mouse = "Mouse position x: " + xpos + " y: " + ypos
            + "\nTrue Cords     x: " + xpos + " y: " + ((ypos - 480) * -1);

    if (namer == 1) {

        if ((xpos > 121 && xpos < 155) && (ypos > 278 && ypos < 318)) { if (Mouse.isButtonDown(0)) {
                sb.append("A"); namer = 2; } } }

    if (namer == 2) {

        if ((xpos > 121 && xpos < 155) && (ypos > 278 && ypos < 318)) { if (Mouse.isButtonDown(0)) {
                sb.append("A"); namer = 3; } } }


    if ((xpos > 650 && xpos < 700) && (ypos > 62 && ypos < 96)) {
        if (Mouse.isButtonDown(0)) { if (namer == 2) { sb.setLength(0); namer = 1;  } } }
    if ((xpos > 650 && xpos < 700) && (ypos > 62 && ypos < 96)) {
        if (Mouse.isButtonDown(0)) { if (namer == 3) { sb.setLength(1); namer = 2;  } } }
    if ((xpos > 650 && xpos < 700) && (ypos > 62 && ypos < 96)) {
        if (Mouse.isButtonDown(0)) { if (namer == 4) { sb.setLength(2); namer = 3;  } } }
    if ((xpos > 650 && xpos < 700) && (ypos > 62 && ypos < 96)) {
        if (Mouse.isButtonDown(0)) { if (namer == 5) { sb.setLength(3); namer = 4;  } } }
    if ((xpos > 650 && xpos < 700) && (ypos > 62 && ypos < 96)) {
        if (Mouse.isButtonDown(0)) { if (namer == 6) { sb.setLength(4); namer = 5;  } } }
    if ((xpos > 650 && xpos < 700) && (ypos > 62 && ypos < 96)) {
        if (Mouse.isButtonDown(0)) { if (namer == 7) { sb.setLength(5); namer = 6;  } } }
    if ((xpos > 650 && xpos < 700) && (ypos > 62 && ypos < 96)) {
        if (Mouse.isButtonDown(0)) { if (namer == 8) { sb.setLength(6); namer = 7;  } } }
    if ((xpos > 650 && xpos < 700) && (ypos > 62 && ypos < 96)) {
        if (Mouse.isButtonDown(0)) { if (namer == 9) { sb.setLength(7);namer = 9;   } } }
}

}

4

1 回答 1

0

如果您阅读setLength 的 Javadoc,您会发现该参数必须为非负数。此方法将 的长度设置为StringBuilder指定的长度。因此,如果要将长度减 1,则需要执行以下操作:

sb.setLength(sb.length() - 1);

请注意,如果StringBuilder为空,此代码将崩溃。因此,您可能需要添加检查:

if (sb.length() > 0) {
    sb.setLength(sb.length() - 1);
}
于 2013-10-12T07:50:40.010 回答