由于某种原因,我可以为字符串做任何方法。这包括:
*获取字符串的长度
*添加到字符串
*使用子字符串
*可能是其他所有人
除了使用我的drawString
方法在lwjgl中绘制到屏幕之外,我无法获取字符串的值。在我进一步解释这个问题之前,这是我的代码。
public static boolean chatOn = false;
public static String text = "";
public static float typetimer = 0;
public static int ctrltimer = 0;
public static boolean runcmd = false;
public static void chat() {
if (Keyboard.isKeyDown(Keyboard.KEY_TAB)) {
if (ctrltimer < 0) {
chatOn = !chatOn;
Keyboard.destroy();
try {Keyboard.create();} catch (LWJGLException e) {}
ctrltimer = 10;
}
}
ctrltimer -= Game.delta;
typetimer -= Game.delta;
if (chatOn) {
//try {text.replace(null, "happy");} catch(NullPointerException e) {}
System.out.println(text);//print to console, dosen't
Text.drawString(text, 0, 0);//write the text on the screen with my draw method, does work
System.out.println(text);//print to console, dosen't, yet the one drawstring worked
if (typetimer < 0) {
while (Keyboard.next()) {
try {
if (Keyboard.getEventKey() == Keyboard.KEY_BACK) {
text = text.substring(0, text.length()-1);
typetimer = 1;
System.out.println(text);//print to console, doesn't
}
else if (Keyboard.getEventKey() == Keyboard.KEY_RETURN) {
System.out.println(text);//print to console, doesn't
runCommand();
text = "";
chatOn = false;
}
else {
System.out.println(text);//print to console, doesn't
text = text + Keyboard.getEventCharacter();
}
typetimer = 10;
} catch(Exception e){
}
}
}
}
}
public static void runCommand() {
String command = text;
System.out.println(command);//print to console, doesnt
if (command.startsWith("time")) {
try {
String[] time = new String[1];
time = command.split(" ", 0);
Camera.nighttimeASecond = Integer.parseInt(time[0]);
} catch (Exception e){
System.out.println("could not set time");
}
}
}
如果您在代码中阅读了我的笔记,您可以看到我放置print
方法和drawString
方法的位置。虽然print
方法工作正常,但drawString
方法没有打印任何内容,有时可能会打印字符串的前几个单词。谢谢 - 泰勒
答案 0 :(得分:0)
如果在调用System.out.println(text);
之前Text.drawString(text, 0, 0);
为空,则在调用text
时Text.drawString()
应为空。您应该follow mattias' suggested debugging guide并找出问题发生的位置(或者在System.out.println()
之后添加一些text
代码,如果您特别懒,则跟踪它:p)。如果没看一下你的Text
类,我愿意打赌要打印的字符串永远不会被设置,Text
正在打印一个静态字符串,或者要打印的字符串正在被Text
类本身改变。
判断:
String command = text;
System.out.println(command);//print to console, doesnt
text
永远不会被设定。