绘制精灵Libgdx

时间:2014-06-16 17:05:18

标签: java libgdx sprite

我正在尝试使用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() {

    }

}

还有一个班级农民为了抓住农夫精灵而烦恼: 包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);
        }


}

1 个答案:

答案 0 :(得分:1)

你得到一个NPE,因为在尝试渲染之前你没有实例化你的精灵。在屏幕的init()方法中(我猜你有一个,因为渲染(浮动)在其中并且你正在使用它),请调用FarmerAsset.load();。这将加载你的纹理就像你问的那样。

[编辑]查看崩溃日志后,我发现您可能正在使用游戏而不是屏幕。不要感到困惑,它与Game implements 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();

}