1

私は見つけたすべての Google の結果をクリックしましたが、成功しなかった可能性があります。

完全な UI を実装したいゲームを作成しました。私のゲームでは、スプライト用に個別のクラスがあるため、画面を作成するためのいくつかのチュートリアルを実行しながら、テクスチャや現在のフレームなどを取得するためにこれらの個別のクラスを使用できるものを実装しようとしました.

2 つの画面がそれ自体以外のものを参照していない場合、2 つの画面を切り替えることができるようになりましたが、ゲーム画面でスプライト クラスを参照しようとするとクラッシュし、ゲーム クラスで次のように指摘されます。

if (screen != null) screen.render(Gdx.graphics.getDeltaTime());

したがって、ここに私が使用しているコードがあります(クラスのデフォルトである実装されたメソッドやその他のものを差し引いたもの)。

チュートリアル.java:

public class Tutorial extends Game {
    MainMenuScreen mainMenuScreen ;
    AnotherScreen anotherScreen;
    Player player ;

    @Override
    public void create() {
        mainMenuScreen = new MainMenuScreen(this);
        anotherScreen = new AnotherScreen(this);
        setScreen(mainMenuScreen);
    }
}

MainMenuScreen.java (問題はバッチにあります):

public class MainMenuScreen implements Screen {
    Tutorial game ;
    SpriteBatch batch ;
    Player player ;
    Texture test ;

    public MainMenuScreen(Tutorial game){
        this.game = game;
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);

        batch.begin();
        batch.draw(player.playerTexture, 200, 200); // error points to here
        batch.end();
    }
}

AnotherScreen.java (正常に実行されます):

public class AnotherScreen implements Screen {
    Tutorial game ;
    SpriteBatch batch ;
    Texture test ;

    public AnotherScreen(Tutorial game){
        this.game = game;
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);

        batch.begin();
        batch.draw(test, 0, 200);
        batch.end();
    }

    @Override
    public void show() {
        batch = new SpriteBatch();
        test = new Texture(Gdx.files.internal("badlogic.jpg")); 
    }
}

Player.java:

public class Player {
    Texture playerTexture ;
    Vector2 position;
    String textureLoc;

    public Player(Vector2 position, String textureLoc){
    //What I am trying to get from AnotherScreen.java
        playerTexture = new Texture(Gdx.files.internal("badlogic.jpg")); 
    }
    public Texture getPlayerTexture() {
        return playerTexture;
    }
    public void setPlayerTexture(Texture playerTexture) {
        this.playerTexture = playerTexture;
    }
}

コンソールの正確なエラーは次のとおりです。

Exception in thread "LWJGL Application" java.lang.NullPointerException
    at com.tutorial.MainMenuScreen.render(MainMenuScreen.java:25)
    at com.badlogic.gdx.Game.render(Game.java:46)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)

私は何を間違っていますか?すべてを別の場所に配置するコードを再配置しましたが、機能させることができません。お時間をいただきありがとうございます!

MainMenuScreen を開いて、AnotherScreen に移動し、Main からテクスチャを取得して Another に表示しようとすると、Tutorial でテクスチャを取得してから Another に渡すと機能することがわかりました。ただし、これはプレーヤーでは機能しません...

4

2 に答える 2

3

AnotherScreen は、使用する前にバッチをインスタンス化したために機能します。ただし、MainMenuScreen では、バッチ オブジェクトとプレイヤー オブジェクトの両方をインスタンス化していません。つまり、NPE です。次のように、バッチ テクスチャとプレーヤー テクスチャをインスタンス化したことを確認します。

public class MainMenuScreen implements Screen {
    Tutorial game ;
    SpriteBatch batch ;
    Player player ;
    Texture test ;

    public MainMenuScreen(Tutorial game){
        this.game = game;
    batch = new SpriteBatch();
    player = new Player(position, textureLoc); //modify arguments
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);

        batch.begin();
        batch.draw(player.playerTexture, 200, 200); // error points to here
        batch.end();
    }
}
于 2014-05-30T20:03:13.123 に答える
1

MainMenuScreen.javaでは、インスタンス化していませんSpriteBatch。ただ行う:

batch = new SpriteBatch();

コンストラクタ内またはコンストラクタ内で使用する前にshow()

編集 :

playerまた、どこにもインスタンス化されていません。

于 2014-05-30T19:50:22.180 に答える