2014-06-16 187 views
-1

我试图使用libgdx维基作为一个exampel基地绘制精灵:https://github.com/libgdx/libgdx/wiki/Spritebatch%2C-Textureregions%2C-and-Sprites 但是我得到这个错误:绘制精灵Libgdx

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

    } 

} 

和A类FarmerAsset举办农民雪碧: 包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); 
     } 


} 
+0

不要在此引用我,但你的精灵可能没有加载。该行实际上是空的? – SneakyB

回答

1

你得到了一个N​​PE,因为你在试图渲染它之前没有实例化你的精灵。在你的屏幕的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(); 

} 
+0

请参阅我的新编辑,并可以扩大您的编辑,对不起,即时通讯非常新的libgdx和java一般:)谢谢你 – user3165683

+0

你去找伙伴 – Ferdz