Android Studio LibGDX项目在移动设备上崩溃,但在桌面

时间:2016-02-22 16:51:33

标签: libgdx

当我在桌面模拟器上运行应用程序时,它运行正常,但是如果我打开我的移动设备,当我按下主菜单上的播放按钮时它会崩溃。在桌面设备上,它会更改为播放屏幕。

我知道它在某个地方是空的,但我找不到它。如果我删除

hud = new Hud(game.batch);

我桌面模拟器上也是黑屏。

戏剧类:

public class PlayScreen implements Screen {
private RunningGame game;
private TextureAtlas atlas;


private OrthographicCamera gamecam;
private Viewport gamePort;
private Hud hud;

private TmxMapLoader maploader;
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;

private World world;
private Box2DDebugRenderer b2dr;
private Runner player;
private float dt;
private boolean keyPressed = false;
private Button speedButton;


public PlayScreen(RunningGame game){
    atlas = new TextureAtlas("Runner_And_Enemies.pack");

    this.game = game;
    gamecam = new OrthographicCamera();
    gamePort = new FitViewport(RunningGame.V_WIDTH / RunningGame.PPM, RunningGame.V_HEIGHT / RunningGame.PPM, gamecam);
    hud = new Hud(game.batch);


    maploader = new TmxMapLoader();
    map = maploader.load("level1.tmx");
    renderer = new OrthogonalTiledMapRenderer(map, 1 / RunningGame.PPM);
    gamecam.position.set(gamePort.getWorldWidth() / 2, gamePort.getWorldHeight() / 2 ,0);

    world = new World(new Vector2(0, -10), true);
    b2dr = new Box2DDebugRenderer();

    new B2WorldCreator(world, map);
    player = new Runner(world, this);


}

public TextureAtlas getAtlas(){
    return atlas;

}

@Override
public void show() {

}

public void handleInput(float dt){

   if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
      if (!keyPressed) {
          keyPressed = true;
           if (player.b2body.getLinearVelocity().x <= 4) {
              player.b2body.applyLinearImpulse(new Vector2(0.3f, 0), player.b2body.getWorldCenter(), true);

         }
            Hud.addscore(1);
        }
    } else {
        keyPressed = false;
   }



    if (Gdx.input.isKeyPressed(Input.Keys.UP))
            player.b2body.applyLinearImpulse(new Vector2(0, 0.4f), player.b2body.getWorldCenter(), true);
    if (Gdx.input.isKeyPressed(Input.Keys.LEFT) && player.b2body.getLinearVelocity().x >= -2)
            player.b2body.applyLinearImpulse(new Vector2(-5.2f, 0), player.b2body.getWorldCenter(), true);


}

public void update(float dt){
    handleInput(dt);

    world.step(1 / 60f, 6, 2);



    player.update(dt);

    gamecam.position.x = player.b2body.getPosition().x;

    gamecam.update();
    renderer.setView(gamecam);

}

@Override
public void render(float delta) {
    update(delta);


    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    renderer.render();

    b2dr.render(world, gamecam.combined);

    game.batch.setProjectionMatrix(gamecam.combined);
    game.batch.begin();
    player.draw(game.batch);
    game.batch.end();

    game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
    hud.stage.draw();



}

@Override
public void resize(int width, int height) {
    gamePort.update(width, height);

}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void hide() {

}

@Override
public void dispose() {
    map.dispose();
    renderer.dispose();
    world.dispose();
    b2dr.dispose();
    hud.dispose();

}
}

正在运行游戏类:

import com.badlogic.gdx.Game;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.mygdx.game.Screens.PlayScreen;
import com.mygdx.game.Screens.SplashScreen;

import aurelienribon.tweenengine.TweenManager;


public class RunningGame extends Game {
    public static final int V_WIDTH = 400;
    public static final int V_HEIGHT = 208;
    public static final float PPM = 100;
    public static final String TITLE = "Running Game", VERSION = "0.0.0.0.reallyEarly";
    public SpriteBatch batch;
    private TweenManager tweenManager;


    @Override
    public void create () {
        batch = new SpriteBatch();
        tweenManager = new TweenManager();
        setScreen(new SplashScreen(this));

        }

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

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

    }

    @Override
    public void resize(int width, int height) {

    }

    @Override
    public void pause(){

    }

    @Override
    public void resume(){

    }

}

错误日志:

 FATAL EXCEPTION: GLThread 26852
                                                                    Process: com.mygdx.game, PID: 17739
                                                                    java.lang.NullPointerException: Attempt to read from field 'com.badlogic.gdx.graphics.g2d.SpriteBatch com.mygdx.game.RunningGame.batch' on a null object reference
                                                                        at com.mygdx.game.Screens.PlayScreen.<init>(PlayScreen.java:55)
                                                                        at com.mygdx.game.Screens.MainMenu$1$1.run(MainMenu.java:86)
                                                                        at com.badlogic.gdx.scenes.scene2d.actions.RunnableAction.run(RunnableAction.java:42)
                                                                        at com.badlogic.gdx.scenes.scene2d.actions.RunnableAction.act(RunnableAction.java:32)
                                                                        at com.badlogic.gdx.scenes.scene2d.actions.SequenceAction.act(SequenceAction.java:65)
                                                                        at com.badlogic.gdx.scenes.scene2d.Actor.act(Actor.java:95)
                                                                        at com.badlogic.gdx.scenes.scene2d.Group.act(Group.java:46)
                                                                        at com.badlogic.gdx.scenes.scene2d.Stage.act(Stage.java:222)
                                                                        at com.mygdx.game.Screens.MainMenu.render(MainMenu.java:47)
                                                                        at com.badlogic.gdx.Game.render(Game.java:46)
                                                                        at com.mygdx.game.RunningGame.render(RunningGame.java:36)
                                                                        at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:472)
                                                                        at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1561)
                                                                        at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1278)
    02-22 18:36:51.388 17739-17739/com.mygdx.game E/AndroidGraphics: waiting for pause synchronization took too long; assuming deadlock and killing
    02-22 18:36:51.388 17739-17739/com.mygdx.game I/Process: Sending signal. PID: 17739 SIG: 9

0 个答案:

没有答案