Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 绘制sprite Libgdx_Java_Libgdx_Sprite - Fatal编程技术网

Java 绘制sprite Libgdx

Java 绘制sprite Libgdx,java,libgdx,sprite,Java,Libgdx,Sprite,我正在尝试使用libgdx wiki绘制一个精灵作为示例的基础: 但我得到了这个错误: 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.

我正在尝试使用libgdx wiki绘制一个精灵作为示例的基础: 但我得到了这个错误:

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

    }

}
还有一个用来装农夫精灵的等级: 包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);
        }


}

你得到一个NPE,因为你在尝试渲染之前没有实例化你的精灵。在屏幕的init()方法中(我想您有一个,因为render(float)在其中并且您正在使用它),调用
FarmerAsset.load()。这会像你要求的那样加载你的纹理

[编辑]查看您的崩溃日志后,我发现您可能正在使用游戏而不是屏幕。别弄糊涂了,这和游戏屏幕上的东西差不多

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都是新手:)谢谢