Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在LWJGL/Slick2D中逐字符绘制文本_Java_String_Text_Lwjgl_Slick2d - Fatal编程技术网

Java 如何在LWJGL/Slick2D中逐字符绘制文本

Java 如何在LWJGL/Slick2D中逐字符绘制文本,java,string,text,lwjgl,slick2d,Java,String,Text,Lwjgl,Slick2d,我想问一下,有人知道如何绘制字符串,在间隔内逐字符绘制,例如,0.25秒 "H" (0.25sec) "e" (0.25sec) "l" (0.25sec) "l" (0.25sec) "o"; 我使用drawText(x,y,String)绘制文本。您可以使用System.nanoTime()测量经过的时间 请参阅:要在slick中很好地实现这一点,我建议在游戏中使用update()方法。对于每个更新勾号,您可以有一个变量计数增量经过了多少毫秒(近似值)(更新所需的时间),然后在确定的时间量

我想问一下,有人知道如何绘制字符串,在间隔内逐字符绘制,例如,0.25秒

"H" (0.25sec) "e" (0.25sec) "l" (0.25sec) "l" (0.25sec) "o";

我使用
drawText(x,y,String)绘制文本。

您可以使用System.nanoTime()测量经过的时间


请参阅:

要在slick中很好地实现这一点,我建议在游戏中使用update()方法。对于每个更新勾号,您可以有一个变量计数增量经过了多少毫秒(近似值)(更新所需的时间),然后在确定的时间量(您希望它添加字母的速度)后添加字母

如果你想在整个游戏中使用它,我绝对建议你把它放到自己的类中,比如messageFeed或messageWriter,或者任何你想叫它的东西

下面是我写的一个快速示例:

public class TextFieldTest extends BasicGameState{

    private int stateId = 0;
    String text = "This is a line of text blah blah blah";
    String currentText = "";
    int count = 0;
    int index = 0;

    public TextFieldTest(int State) {
        this.stateId = State;
    }

    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{

    }

    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
        g.drawString(currentText, 40, 40);
    }

    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
        if(count < 25) {
            count+=delta;
        } else {
            count = 0;
            if(index < text.length()) {
                currentText += text.charAt(index++);
            }
        }
    }

    public int getID(){
        return this.stateId;
    }
}
public类TextFieldTest扩展了BasicGameState{
私有int stateId=0;
String text=“这是一行文字废话废话”;
字符串currentText=“”;
整数计数=0;
int指数=0;
公共文本字段测试(int状态){
this.stateId=状态;
}
public void init(GameContainer gc,StateBasedGame sbg)引发异常{
}
公共void呈现(GameContainer gc、StateBasedGame sbg、Graphics g)引发异常{
g、 抽绳(currentText,40,40);
}
公共无效更新(GameContainer gc、StateBasedGame sbg、int delta)引发异常{
如果(计数<25){
计数+=增量;
}否则{
计数=0;
如果(索引
在本例中,我使用25作为阈值,一旦我打破该阈值,我将添加一个新的要显示的字母,然后重新开始计数。比使用系统时间更容易,因为slick已经为您获得了增量。你可以随意处理这个例子来得到你想要的结果,你甚至可以用一个变量来代替25,这样你就可以改变你想要的文本速度