有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

libGdx屏幕出现java错误

我有一个问题,当我试图编译这是supuse,这将是一个游戏

Exception in thread "LWJGL Application" java.lang.NullPointerException at com.waflesgames.spaceinvader.Screens.PlayScreen.render(PlayScreen.java:35) at com.badlogic.gdx.Game.render(Game.java:46) at com.waflesgames.spaceinvader.SpaceInvader.render(SpaceInvader.java:24) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215) at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)

这是播放屏幕。爪哇

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

}
}

这就是太空入侵者。爪哇

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) 个答案

  1. # 1 楼答案

    您在此处遇到错误:

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

    它应该是游戏而不是游戏-您将SpaceInvader实例设置为游戏变量,因此下面您将分配此。游戏本身导致渲染方法中出现nullpointer异常的原因(游戏为null,所以您无法访问其批处理)