libGdx屏幕出错

时间:2015-10-04 22:33:32

标签: java libgdx screen

当我尝试编译时,我遇到了一个问题,这是游戏的过度。

  

线程中的异常" LWJGL应用程序" com.waflesgames.spaceinvader.Screens.PlayScreen.render上的java.lang.NullPointerException(PlayScreen.java:35)   在com.badlogic.gdx.Game.render(Game.java:46)   在com.waflesgames.spaceinvader.SpaceInvader.render(SpaceInvader.java:24)   在com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215)   在com.badlogic.gdx.backends.lwjgl.LwjglApplication $ 1.run(LwjglApplication.java:120)

这是PlayScreen.java

package com.waflesgames.spaceinvader.Screens;

import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.waflesgames.spaceinvader.SpaceInvader;

import javax.xml.soap.Text;

/**
* Created by Diego on 03/10/2015.
*/
public class PlayScreen implements Screen{

public SpaceInvader game;
Texture texture;
private OrthographicCamera gamecam;
private Viewport gamePort;
public PlayScreen(SpaceInvader Game){
    this.game = game;
    texture = new Texture("badlogic.jpg");
    gamecam = new OrthographicCamera();
    gamePort = new ScreenViewport(gamecam);
}

@Override
public void show() {

}

@Override
public void render(float delta) {
    game.batch.setProjectionMatrix(gamecam.combined);
    game.batch.begin();
    game.batch.draw(texture,0,0);
    game.batch.end();
}

@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() {

}
}

这是SpaceInvader.java

package com.waflesgames.spaceinvader;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.waflesgames.spaceinvader.Screens.PlayScreen;

public class SpaceInvader extends Game {
public  SpriteBatch batch;


@Override
public void create () {
    batch = new SpriteBatch();
    setScreen(new PlayScreen(this));
}

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

1 个答案:

答案 0 :(得分:4)

你在这里有错误:

    public PlayScreen(SpaceInvader Game){
        this.game = game;
        ...

它应该是游戏而不是游戏 - 你得到了SpaceInvader实例到Game变量,所以下面你将this.game分配给它自己导致渲染中出现nullpointer异常的原因方法(游戏为空,因此您无法访问其批次)