I'm trying to add some text effects to my game by making the text "type" Here, maybe the pseudocode will make it understandable.
String text = "But what I do have are a very particular set of skills, skills I have acquired over a very long career.";
char = characters(text) ///turn string into list/array of letters
i = 0; //initializes i
while (i < text.length) {
print(char.letter[i]) ///print 'i'th letter in list (starting with 1)
TimeUnit.MILLISECONDS.sleep(100) //wait 1/10th of second
i++; //repeat for all letters
}
P.S. comments with triple slashes are things i don't know how to do
答案 0 :(得分:2)
只需使用for-each loop代替输入文字:
String text = "...";
for(char c : text.toCharArray()) {
System.out.print(c);
Thread.sleep(100);
}
System.out.println();
答案 1 :(得分:0)
虽然Sasha's answer已经概述如何这样做,但要创建一个非常漂亮的打字效果,重要的是不要总是在按键之间等待相同的时间。
人类以不同的速度进行打字。通常他们在特殊字母之前有更长的停顿时间,这些字母位于键盘上不方便的地方(想想'z','`'和类似的东西)并在开始一个新单词(或新句子)之前暂停一段时间
因此,您应该为最佳的“打字”体验添加随机化到您在游戏中的睡眠时间。
请考虑以下事项:
String text = "...";
for (char c : text.toCharArray()) {
// additional sleeping for special characters
if (specialChars.contains(c)) {
Thread.sleep(random.nextInt(40) + 15);
}
System.out.print(c);
Thread.sleep(random.nextInt(90) + 30);
}
这些数字可能会使用一些微调,但这应该给你一个必要的要点