libGDX出错,窗口立即打开和关闭

时间:2018-05-09 20:20:46

标签: java compiler-errors libgdx window

这是我的错误:

Exception in thread "LWJGL Application" java.lang.NullPointerException
    at player.Player.updatePlayer(Player.java:45)
    at scenes.MainMenu.render(MainMenu.java:38)
    at com.badlogic.gdx.Game.render(Game.java:46)
    at com.mygdx.gamemain.GameMain.render(GameMain.java:18)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:225)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)

任何想法?它会打开一个窗口并立即将其关闭,从而给我带来上述错误。我正在使用android studio。

GameMain.java - :

package com.mygdx.gamemain;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import scenes.MainMenu;

public class GameMain extends Game {
    private SpriteBatch batch;

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

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

    public SpriteBatch getBatch() {
        return  this.batch;
    }
}

Player.java - :

package player;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;

import java.awt.Polygon;

public class Player extends Sprite {

    private World world;
    private Body body;

    public Player(World world, String name, float x, float y) {
        super(new Texture(name));
        this.world = world;
        setPosition(x - getWidth() / 2, y - getHeight() / 2);
    }

    void createBody() {
        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyDef.BodyType.DynamicBody;
        bodyDef.position.set(getX(), getY());

        body = world.createBody(bodyDef);

        PolygonShape shape = new PolygonShape();
        shape.setAsBox(getWidth() / 2, getHeight() / 2);

        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = shape;
        fixtureDef.density = 1f;

        Fixture fixture = body.createFixture(fixtureDef);

        shape.dispose();
    }

    public void updatePlayer() {
        this.setPosition(body.getPosition().x, body.getPosition().y);
    }
}

MainMenu.java - :

package scenes;

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.math.Vector2;
import com.badlogic.gdx.physics.box2d.World;
import com.mygdx.gamemain.GameMain;

import helpers.GameInfo;
import player.Player;

public class MainMenu implements Screen {

    private GameMain game;
    private Player player;
    private Texture bg;
    private World world;

    public MainMenu (GameMain game) {
        this.game = game;

        world = new World(new Vector2(0, -9.8f), true);

        bg = new Texture("Backgrounds/background.png");
        player = new Player(world, "Character/Char_Idle/P1.png", GameInfo.WIDTH / 2,
            GameInfo.HEIGHT / 2);
    }

    @Override
    public void show() {

    }

    @Override
    public void render(float delta) {
        player.updatePlayer();

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

        game.getBatch().begin();
        game.getBatch().draw(bg, 0, 0);
        game.getBatch().draw(player, player.getX(), player.getY() );
        game.getBatch().end();

        world.step(Gdx.graphics.getDeltaTime(), 6, 2);
    }

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

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {
        bg.dispose();
        player.getTexture().dispose();
    }
}

GameInfo.java - :

package helpers;

public class GameInfo {
    public static final int WIDTH = 360;
    public static final int HEIGHT = 640;
}

DesktopLauncher - :

package com.mygdx.gamemain.desktop;

import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.mygdx.gamemain.GameMain;
import helpers.GameInfo;

public class DesktopLauncher {
    public static void main (String[] arg) {
        LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
        new LwjglApplication(new GameMain(), config);
        config.width = GameInfo.WIDTH;
        config.height = GameInfo.HEIGHT;
    }
}

那是我的整个项目,最相关的部分是Player.java和MainMenu.java。

提前thx !!

0 个答案:

没有答案