【问题标题】:Drawing a sprite Libgdx绘制精灵 Libgdx
【发布时间】:2023-03-31 00:46:02
【问题描述】:

我正在尝试使用 libgdx wiki 作为示例的基础来绘制精灵:https://github.com/libgdx/libgdx/wiki/Spritebatch%2C-Textureregions%2C-and-Sprites 但我得到这个错误:

Exception in thread "LWJGL Application" java.lang.NullPointerException
    at com.MKgames.OptionScreen.render(OptionScreen.java:44)
    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)

为 2d 游戏绘制精灵的最佳方法是什么,最佳实践是什么???

这是我的选项屏幕类,包括渲染方法:

    package com.MKgames;

import sun.java2d.loops.DrawGlyphListAA.General;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class OptionScreen implements Screen{

    com.MKgames.Game1 game;
    OrthographicCamera camera;
    SpriteBatch batch;


    int farmerX;

    public OptionScreen(com.MKgames.Game1 game1){
        this.game = game;

        camera = new OrthographicCamera();
        camera.setToOrtho(true, 1920, 1080);

        batch = new SpriteBatch();

        farmerX = 960-85;

    }


    public void render(float delta) {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);


        camera.update();
        generalUpdate();
        batch.setProjectionMatrix(camera.combined);

        batch.begin();
            FarmerAsset.farmer1.draw(batch);
        batch.end();

    }


    public void generalUpdate() {
        if(Gdx.input.isKeyPressed(Keys.A)||Gdx.input.isKeyPressed(Keys.LEFT)){
            farmerX -= 5;
        }
        else if(Gdx.input.isKeyPressed(Keys.D)||Gdx.input.isKeyPressed(Keys.RIGHT)){
            farmerX += 5;   
        }

    }


    @Override
    public void show() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {

    }

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

    }

    @Override
    public void hide() {

    }

}

还有一个 FarmerAsset 类来保存农民 Sprite: 包 com.MKgames;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;

public class FarmerAsset {

    public static Texture texture_sheet;
    public static Sprite farmer1;
    public static Sprite farmer2;
    public static Sprite farmer3;
    public static Sprite farmer4;
    public static Sprite farmer5;
    public static Sprite farmer6;
    public static Sprite farmer7;
    public static Sprite farmer8;

    public static void load(){
        texture_sheet = new Texture(Gdx.files.internal("farmerSprite.png"));
        farmer1 = new Sprite (texture_sheet, 0, 0, 170, 170);
        farmer2 = new Sprite (texture_sheet, 170, 0, 170, 170);
        farmer3 = new Sprite (texture_sheet, 340, 0, 170, 170);
        farmer4 = new Sprite (texture_sheet, 680, 0, 170, 170);
        farmer5 = new Sprite (texture_sheet, 0, 170, 170, 170);
        farmer6 = new Sprite (texture_sheet, 170, 170, 170, 170);
        farmer7= new Sprite (texture_sheet, 340, 170, 170, 170);
        farmer8= new Sprite (texture_sheet, 680, 170, 170, 170);
        farmer1.flip(false, true);
        farmer2.flip(false, true);
        farmer3.flip(false, true);
        farmer4.flip(false, true);
        farmer5.flip(false, true);
        farmer6.flip(false, true);
        farmer7.flip(false, true);
        farmer8.flip(false, true);
        }


}

【问题讨论】:

  • 不要引用我的话,但你的精灵可能没有加载。那一行实际上是什么?

标签: java libgdx sprite


【解决方案1】:

你得到一个 NPE 是因为你在尝试渲染之前没有实例化你的 sprite。在屏幕的 init() 方法中(我猜你有一个,因为 render(float) 在其中并且你正在使用它),调用FarmerAsset.load();。这将按照您的要求加载您的纹理。

[编辑] 查看您的崩溃日志后,我发现您可能使用的是游戏而不是屏幕。别搞混了,跟Game实现Screen差不多。

public OptionScreen(com.MKgames.Game1 game1){
    this.game = game;

    camera = new OrthographicCamera();
    camera.setToOrtho(true, 1920, 1080);

    batch = new SpriteBatch();

    farmerX = 960-85;

    //This is what's missing
    FarmerAsset.load();

}

【讨论】:

  • 请看我的新编辑,你能扩展你的编辑吗,对不起,我对 libgdx 和 java 很陌生 :) 谢谢你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-27
  • 2016-03-13
  • 1970-01-01
相关资源
最近更新 更多