java - LibGDX SpriteBatch渲染错误

标签 java libgdx sprite render spritebatch

所以早些时候,当我测试我的游戏时,一切都很顺利,然后我添加了一些代码。在我的游戏中,我有 PlayState 或 MenuState 等状态,现在一切顺利,但是当我尝试在游戏中切换状态时,会出现以下错误:

Exception in thread "LWJGL Application" java.lang.NullPointerException
    at com.badlogic.gdx.graphics.g2d.SpriteBatch.flush(SpriteBatch.java:955)
    at com.badlogic.gdx.graphics.g2d.SpriteBatch.end(SpriteBatch.java:176)
    at com.platformer.Entities.Player.draw(Player.java:191)
    at com.platformer.gamestates.PlayState.draw(PlayState.java:102)
    at com.platformer.managers.GameStateManager.draw(GameStateManager.java:44)
    at com.platformer.game.Game.render(Game.java:55)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:214)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)

它说问题出在我的 Player 类中,所以这里是代码:

package com.platformer.Entities;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import static com.platformer.managers.B2DVars.PPM;

import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.platformer.game.Game;
import com.platformer.managers.Animation;
import com.platformer.managers.B2DVars;
import com.platformer.managers.GameContacts;

import java.util.Random;

/**
 * Created by Oliverss on 23/04/2015.
 */
public class Player extends Entities{

    public static boolean DebugOn=false;
    public boolean shouldPlay=false;
    public boolean shouldMenu=false;

    public static final int LEFT = 0;
    public static final int RIGHT = 1;
    public int direction=1;

    float BPF=255f;
    int canJump=0;
    float weaponY;

    public static int shade=100;
    public static int coatColor;

    Controller controller;
    Weapons weapon;

    Random random=new Random();

    float[][] skinShades={
            {210/BPF,180/BPF,140/BPF},  //tan
            {139/BPF,69/BPF,19/BPF},    //saddle-brown
            {160/BPF,82/BPF,45/BPF},    //sienna
            {255/BPF,235/BPF,205/BPF},  //blanched almond
            {255/BPF,228/BPF,196/BPF},  //bisque
    };

    float[][] coatColors={
            {64/BPF,64/BPF,64/BPF},  //dark-gray
            {102/BPF,102/BPF,0/BPF},  //dark-green
            {47/BPF,79/BPF,79/BPF},   //dark-slate-gray
            {165/BPF,42/BPF,42/BPF},    //brown
    };

    public Player(World world,float x,float y){

        this.x=x;
        this.y=y;
        speed=1.5f;

        if(shade==100){
            shade=random.nextInt(skinShades.length);
            coatColor=random.nextInt(coatColors.length);
            System.out.println("*Generating*");
            System.out.println("shade "+shade);
            System.out.println("coat "+coatColor);
        }

        controller=new Controller();
        weapon=new Weapons("pistol");

        AtlasRegion region=Game.atlas.findRegion("playerskin");
        TextureRegion[] sprites=region.split(32,32)[0];
        skinAnimation=new Animation(sprites,1/12f);

        region=Game.atlas.findRegion("playercoat");
        sprites=region.split(32,32)[0];
        coatAnimation=new Animation(sprites,1/12f);

        region=Game.atlas.findRegion("playerskinjump");
        sprites=region.split(32,32)[0];
        skinJumpAnimation=new Animation(sprites,1/12f);

        region=Game.atlas.findRegion("playercoatjump");
        sprites=region.split(32,32)[0];
        coatJumpAnimation=new Animation(sprites,1/12f);

        skin=new Sprite();
        skin.setSize(32/PPM,32/PPM);

        coat=new Sprite();
        coat.setSize(32/PPM,32/PPM);

        bdef=new BodyDef();
        bdef.position.set(x/PPM,y/PPM);
        bdef.type=BodyType.DynamicBody;
        body=world.createBody(bdef);

        PolygonShape shape=new PolygonShape();
        shape.setAsBox(9/PPM,15/PPM);

        fdef=new FixtureDef();
        fdef.shape=shape;
        fdef.filter.categoryBits=B2DVars.BIT_PLAYER;
        fdef.filter.maskBits=B2DVars.BIT_BLOCK|B2DVars.BIT_HOUSE;
        body.createFixture(fdef).setUserData("player");

        //create sensor
        shape.setAsBox(7/PPM,6/PPM,new Vector2(0,-16/PPM),0);
        fdef.shape=shape;
        fdef.filter.categoryBits=B2DVars.BIT_PLAYER;
        fdef.filter.maskBits=B2DVars.BIT_BLOCK;
        fdef.isSensor=true;
        body.createFixture(fdef).setUserData("player_sensor");

    }

    public void update(float dt,GameContacts gameContacts){

        handleInput(dt,gameContacts);

        skin.setPosition(body.getPosition().x-(16/PPM),body.getPosition().y-(17/PPM));
        coat.setPosition(body.getPosition().x-(16/PPM),body.getPosition().y-(17/PPM));

        switch (movementState){
            case 0:{
                coatAnimation.setCurrentFrame(0);
                skinAnimation.setCurrentFrame(0);
                coatJumpAnimation.setCurrentFrame(0);
                skinJumpAnimation.setCurrentFrame(0);
                coat.setRegion(coatAnimation.getFrame());
                skin.setRegion(skinAnimation.getFrame());
                break;
            }
            case 1:{
                coatAnimation.update(dt);
                skinAnimation.update(dt);
                coat.setRegion(coatAnimation.getFrame());
                skin.setRegion(skinAnimation.getFrame());
                break;
            }
            case 2:{
                coatAnimation.update(dt);
                skinAnimation.update(dt);
                coat.setRegion(coatAnimation.getFrame());
                skin.setRegion(skinAnimation.getFrame());
                break;
            }
            case 3:{
                coatAnimation.setCurrentFrame(0);
                skinAnimation.setCurrentFrame(0);
                coat.setRegion(coatJumpAnimation.getFrame());
                skin.setRegion(skinJumpAnimation.getFrame());
                break;
            }

        }

        y=coat.getY();
        x=coat.getX();
        weaponY=y;

        if(!gameContacts.isPlayerOnGround()){
            coatJumpAnimation.setCurrentFrame(1);
            skinJumpAnimation.setCurrentFrame(1);
            coat.setRegion(coatJumpAnimation.getFrame());
            skin.setRegion(skinJumpAnimation.getFrame());
            weaponY+=4/PPM;
        }

        weapon.update(x,weaponY,direction,coatAnimation.getCurrentFrame());

    }

    public void draw(SpriteBatch batch){

        if(direction==LEFT)flip();

        batch.begin();
        skin.draw(batch);
        coat.draw(batch);
        batch.end();

        weapon.draw(batch);

        controller.debug(DebugOn);
        controller.draw();

    }

    public void flip(){

        skin.flip(true,false);
        coat.flip(true,false);

    }

    public void handleInput(float dt,GameContacts gameContacts){

        //movement
        if(controller.leftDown||Gdx.input.isKeyPressed(Input.Keys.LEFT))movementState=1;
        if(controller.rightDown||Gdx.input.isKeyPressed(Input.Keys.RIGHT))movementState=2;
        if(!controller.leftDown&&!controller.rightDown&&!Gdx.input.isKeyPressed(Input.Keys.LEFT)&&!Gdx.input.isKeyPressed(Input.Keys.RIGHT))movementState=0;

        Vector2 vec=body.getLinearVelocity();
        force=0;

        switch (movementState){
            case 1:if(vec.x>-speed)force=-50;direction=LEFT;break;
            case 2:if(vec.x<speed)force=50;direction=RIGHT;break;
            case 0:force=vec.x*-10;break;
        }

        body.applyForceToCenter(new Vector2(force,0),true);

        if((controller.jumpDown||Gdx.input.isKeyPressed(Input.Keys.UP))&&gameContacts.isPlayerOnGround()&&canJump>50){
            body.applyForceToCenter(0,300,true);
            canJump=0;
        }
        if(canJump>51)canJump=51;
        canJump++;
        if(canJump<52)movementState=3;

        if(controller.attackdown&&gameContacts.isOnHouse()){
            shouldPlay=true;
        }

        if(Gdx.input.isKeyJustPressed(Input.Keys.C)){
            shade=random.nextInt(skinShades.length);
            coatColor=random.nextInt(coatColors.length);
            System.out.println("shade "+shade);
            System.out.println("coat "+coatColor);
        }

        skin.setColor(skinShades[shade][0],skinShades[shade][1],skinShades[shade][2],1);
        coat.setColor(coatColors[coatColor][0],coatColors[coatColor][1],coatColors[coatColor][2],1);

        //debug
        if(controller.attackdown&&(Gdx.input.isKeyJustPressed(Input.Keys.VOLUME_DOWN)||Gdx.input.isKeyJustPressed(Input.Keys.VOLUME_UP)||Gdx.input.isKeyJustPressed(Input.Keys.CONTROL_LEFT))){if(DebugOn){DebugOn=false;System.out.println("off");}else{DebugOn=true;System.out.println("on");}}
        //debug
        if(controller.jumpDown&&(Gdx.input.isKeyJustPressed(Input.Keys.VOLUME_DOWN)||Gdx.input.isKeyJustPressed(Input.Keys.VOLUME_UP)||Gdx.input.isKeyJustPressed(Input.Keys.CONTROL_LEFT))){shouldMenu=true;}
    }

    @Override
    public void dispose(){
        skin.getTexture().dispose();
        coat.getTexture().dispose();
        weapon.dispose();
    }

    public float getx(){return x;}
    public float gety(){return y;}

}

这里有什么问题吗?

最佳答案

您确定skincoat在绘制时应用了纹理吗?

您的错误消息表明问题出在此处:

batch.begin();
skin.draw(batch);
coat.draw(batch);
batch.end();

我唯一能想到的就是状态是否困惑并且他们没有纹理可绘制。

关于java - LibGDX SpriteBatch渲染错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30376552/

相关文章:

java - 元语言,如 BNF 或 XML-Schema,用于根据树模型验证树实例

java - libGDX 口袋妖怪式移动(移动前转身)

java - libgdx:屏幕键盘上的 IOS 未持续触发事件

html - CSS Sprite 和屏幕外的链接

python - Pygame 中不显示 Sprite

java - 如何发出 Http Get 快速请求?

java - CORS和错误以及Access-Control-Allow-Origin header 的问题

java - 如果我有多个线程要插入MySQL,我是否需要同步?

gwt - HTML 版本的 LibGDX 游戏不显示任何日志记录

unity-game-engine - Unity - 如何在运行时更改玩家 Sprite 表?