我试图让它变成多个单词(随机生成)出现在屏幕顶部并落到底部。这意味着在屏幕上多次绘制字体。但是每当出现一个新单词时,它应该将该单词更改为与前一单词不同的单词。这有效,但由于某种原因,它也会改变出现的第一个单词。
这是我的代码:
PlayState类:
public class PlayState extends State {
// TODO: add a background
private BitmapFont font;
private Word word;
public PlayState(GameStateManager gsm) {
super(gsm);
cam.setToOrtho(false, Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
// TODO: change the font of the random word
font = new BitmapFont();
font.setColor(Color.BLACK);
word = new Word();
}
@Override
protected void handleInput() {
}
@Override
public void update(float dt) {
handleInput();
}
@Override
public void render(SpriteBatch batch) {
// TODO: make it so it changes the next word that spawns
// Check how much time has passed since a new word has been spawned
// and create a new one if necessary
if (TimeUtils.nanoTime() - word.lastWordTime > 1000000000) {
word.spawnWord();
}
Iterator<Rectangle> iter = word.words.iterator();
while (iter.hasNext()) {
Rectangle word = iter.next();
word.y -= 200 * Gdx.graphics.getDeltaTime(); // move the words at a constant speed of 200 pixels/units per second
// If the word reaches the bottom, remove it from the array
if (word.y + 64 < 0) {
iter.remove();
}
}
batch.setProjectionMatrix(cam.combined);
batch.begin();
for (Rectangle word1 : word.words) {
font.draw(batch, word.getWordString(), word1.x, word1.y);
}
batch.end();
}
@Override
public void dispose() {
font.dispose();
}
}
词类:
public class Word {
public Array<Rectangle> words;
public long lastWordTime;
private FileHandle file, file2;
private BufferedReader reader, reader2;
private List<String> lines, lines2;
private String line, line2;
private Random random;
private String wordString;
public Word(){
words = new Array<Rectangle>();
spawnWord();
}
// Set the new Rectangle to a random position at the top of the screen
// and adds it to the words array
public void spawnWord() {
Rectangle word = new Rectangle();
word.x = MathUtils.random(0, Gdx.graphics.getWidth() / 2 - 64);
word.y = Gdx.graphics.getHeight();
words.add(word);
lastWordTime = TimeUtils.nanoTime();
// Read the file and put it into a list of strings
file = Gdx.files.internal("words/wordsEn.txt");
file2 = Gdx.files.internal("words/swearWords.txt");
reader = new BufferedReader(file.reader());
reader2 = new BufferedReader(file2.reader());
lines = new ArrayList<String>();
lines2 = new ArrayList<String>();
try {
line = reader.readLine();
line2 = reader2.readLine();
} catch (IOException e) {
e.printStackTrace();
}
while (line != null && line2 != null){
lines.add(line);
lines2.add(line2);
try {
line = reader.readLine();
line2 = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
}
// Choose a random string from the list
random = new Random();
wordString = lines.get(random.nextInt(lines.size()));
// Filter out bad words
if (lines2.contains(wordString)) {
wordString = lines.get(random.nextInt(lines.size()));
}
}
public String getWordString(){
return wordString;
}
}
我想我把它缩小到PlayState类中的render方法:
batch.begin();
for (Rectangle word1 : word.words) {
font.draw(batch, word.getWordString(), word1.x, word1.y);
}
batch.end();
它从方法getWordString()获取正确的值,但它改变了与屏幕上的字体有关的所有内容,而不仅仅是新出现的单词。
编辑:
在尝试实施Sneh的例子之后:
public void spawnWord() {
wordRectangle.word.x = MathUtils.random(0, Gdx.graphics.getWidth() / 2 - 64);
wordRectangle.word.y = Gdx.graphics.getHeight();
words.add(wordRectangle.word);
lastWordTime = TimeUtils.nanoTime();
我在words.add(wordRectangle.word)
上收到一条错误,指出&#34;矩形无法转换为WordRectangle&#34;。我想我正在添加错误的东西。
答案 0 :(得分:0)
这是因为您正在更新绘图调用中引用的wordString
变量。因此,当您更新它的价值时,它会反映在任何地方。
对此的修复方法是将矩形和单词保存在单独的类中。
public class WordRectangle {
public String word;
public Rectangle rectangle;
}
现在,在你的word类中有一个上面类的集合,然后在你生成一个新单词时将新对象添加到集合中。
然后在你的渲染方法
batch.begin();
for (WordRectangle word1 : word.words) {
font.draw(batch, word1.word, word1.rectangle.x, word1.rectangle.y);
}
batch.end();
我还建议你阅读this有用的文章来清除我们的内容。