簡體   English   中英

libGDX:如何在按下鍵后逐行讀取和顯示.txt文件中的文本?

[英]libGDX: How to read and display text from a .txt file line by line after pressing a key?

我是Java的新手,正在嘗試制作Visual Novel類型的游戲。 我想做的是像通常的小說一樣,通過按空格鍵逐行顯示文本文件中的文本。 我忽略了不同的方法,但沒有任何結果適合我。 我知道有Scanner和BufferedReader,但是它們適合我在做什么嗎? 現在發生的一切是當我按下“空格”時,整個文本出現在屏幕上,而當我放開時,它消失了。

這是我的游戲課:

public class NovelGame extends Game {

public static final int WIDTH = 1366;
public static final int HEIGHT = 768;

public SpriteBatch batch;
public String txtFileString;
public String lines[];
public BitmapFont bitmapFont;


@Override
public void create () {
    batch = new SpriteBatch();
    bitmapFont = new BitmapFont();
    this.setScreen(new MainMenuScreen(this));
    txtFileString = Gdx.files.internal("script.txt").readString();
    String[] anArray = txtFileString.split("\\r?\\n");}



@Override
public void render () {
    super.render();
}}

這是我的游戲屏幕類:

public class MainGameScreen implements Screen{

    private Texture img;

    NovelGame game;

    public MainGameScreen (NovelGame game) {
        this.game = game;
    }

    @Override
    public void show() {
        img = new Texture("bck.jpg");

    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // This cryptic line clears the screen.
        game.batch.begin();
        game.batch.draw(img, 0, 0);
        if(Gdx.input.isKeyPressed(Input.Keys.SPACE)){
        game.bitmapFont.draw(game.batch, game.txtFileString,Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2);
        }
        game.batch.end();

    }

我有一個想法,可以使用while和if來顯示它,但是無法確切地知道如何去做。

編輯:我找到了一種顯示文本的方法,但是現在,一旦我運行游戲,在按空格鍵后,它就會無休止地開始堆疊。

碼:

public class MainGameScreen implements Screen{

private Texture img;    
NovelGame game;

public MainGameScreen (NovelGame game) {
    this.game = game;
}

@Override
public void show() {
    img = new Texture("bck.jpg");


}

@Override
public void render(float delta) {

    try {
        BufferedReader br = new BufferedReader (new FileReader("C:\\Users\\hydra\\Documents\\JAVA_PROJECTS\\gamespace\\core\\assets\\startd.txt")); 

        while((game.strLine = br.readLine()) != null){
            game.list.add(game.strLine);
     }
        br.close();

     } catch (IOException e) {
         System.err.println("Unable to read the file.");
    }

    if(Gdx.input.isKeyJustPressed(Input.Keys.SPACE)){
        game.showText = true;
    }

    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // This cryptic line clears the screen.
    game.batch.begin();
    game.batch.draw(img, 0, 0);


             if(game.showText) {
                 for(int i = 0; i < game.list.size(); i++) {
                System.out.println(game.list.get(i));    
                game.bitmapFont.draw(game.batch, game.list.get(i), 100, 50);
             }
            }

    game.batch.end();


}       

我最終發現自己在做什么錯。 我把所有東西都放在了render方法中,這就是為什么文件被不斷讀取的原因。 我只需要在構造函數類中執行一次,然后將其放入render方法。

問題在這里。 如果當前只有按鍵,則Gdx.input.isKeyPressed返回true

if(Gdx.input.isKeyPressed(Input.Keys.SPACE)){
    game.bitmapFont.draw(game.batch, game.txtFileString,Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2);
}

要解決此問題,請添加一個布爾標志,僅在按下鍵后將其激活:

if(Gdx.input.isKeyJustPressed(Input.Keys.SPACE)){
    showText = true;
}

並在render方法中:

if (showText) {
   game.bitmapFont.draw(game.batch, game.txtFileString,Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM