Java LWJGL / Slick Util Java空指针异常错误(纹理加载)

时间:2016-01-07 22:47:52

标签: java lwjgl slick2d

我在LWJGL / Slick Util中加载纹理时遇到了一些问题。我在texture.bind()方法中收到此错误。如果需要改进,我还想了解如何改进我的代码。这是:

精灵类:

package geniushour.engine.animation;

import static org.lwjgl.opengl.GL11.*;

import java.io.IOException;

import org.newdawn.slick.opengl.Texture;

public class Sprite {

    private float sx;
    private float sy;
    private Texture texture;

    public Sprite(float sx, float sy, Texture texture) throws IOException{
        this.texture = texture;
        this.sx = sx;
        this.sy = sy;
    }

    public void render(){
        texture.bind();
        glBegin(GL_QUADS);
        glTexCoord2f(0,0);
        glVertex2f(0,0);
        glTexCoord2f(1,0);
        glVertex2f(0,sy);
        glTexCoord2f(1,1);
        glVertex2f(sx,sy);
        glTexCoord2f(0,1);
        glVertex2f(sx,0);
        glEnd();
    }

    public float getSX(){
        return sx;
    }
    public float getSY(){
        return sy;
    }
    public Texture getTexture(){
        return texture;
    }
    public void setSX(float sx){
        this.sx = sx;
    }
    public void setSY(float sy){
        this.sy = sy;
    }
    /*public void setSpriteTexture(String key) throws IOException{
        this.texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("img/"+key+".png"));
    }*/
}

GameObject类:

package geniushour.gameobject;

import static org.lwjgl.opengl.GL11.glPopMatrix;
import static org.lwjgl.opengl.GL11.glPushMatrix;
import static org.lwjgl.opengl.GL11.glTranslatef;

import java.io.IOException;

import org.newdawn.slick.opengl.Texture;

import geniushour.engine.animation.Sprite;

public abstract class GameObject {

    protected float x;
    protected float y;
    protected float sx;
    protected float sy;
    protected Sprite spr;
    protected int type;
    protected static final int PLAYER_ID = 0;
    protected static final int ENEMY_ID = 1;
    protected static final int ITEM_ID = 2;
    protected static final int ENTITY_SIZE = 64;
    protected static final int ITEM_SIZE = 16;
    protected boolean[] flags = new boolean[1];

    public void update(){}
    public void render(){
        glPushMatrix();
        glTranslatef(x,y,0);
        spr.render();
        glPopMatrix();
    }
    public float getX(){
        return x;
    }
    public float getY(){
        return y;
    }
    public float getSX(){
        return spr.getSX();
    }
    public float getSY(){
        return spr.getSY();
    }
    public int getType(){
        return type;
    }
    public boolean getRemove(){
        return flags[0];
    }
    public void remove(){
        flags[0] = true;
    }
    /*protected void setTexture(String key) throws IOException{
        spr.setSpriteTexture(key);
    }*/
    public Texture getTexture(){
        return spr.getTexture();
    }
    protected void init(float x, float y, float sx, float sy,Texture texture,int type) throws IOException{
        this.x = x;
        this.y = y;
        this.type = type;
        this.spr = new Sprite(sx,sy,texture);
    }
}

玩家类:

package geniushour.gameobject.entity;

import java.io.IOException;

import org.lwjgl.input.Keyboard;
import org.newdawn.slick.opengl.Texture;

import geniushour.game.Inventory;
import geniushour.game.Stats;
import geniushour.gameobject.GameObject;
import geniushour.gameobject.item.Item;

public class Player extends GameObject {

    private Stats stats;
    private Inventory inv;
    private int stamina;
    private int maxStamina = 150;

    public Player(float x, float y, Texture texture) throws IOException{
        init(x,y,ENTITY_SIZE,ENTITY_SIZE,texture,PLAYER_ID);
        stats = new Stats(0,true);
        inv = new Inventory(9);
        stamina = maxStamina;
    }
    public void update(){
        if(Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) && stamina > 0){
            stamina--;
        }
        else if(!Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)){
            stamina++;
            if(stamina > maxStamina){
                stamina = maxStamina;
            }
        }
    }
    public void getInput(){
        if(Keyboard.isKeyDown(Keyboard.KEY_W)){
            if(Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) && stamina > 0){
                move(0,1.5);
            }
            else{
                move(0,1);
            }
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_S)){
            if(Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) && stamina > 0){
                move(0,-1.5);
            }
            else{
                move(0,-1);
            }
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_A)){
            if(Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) && stamina != 0){
                move(-1.5,0);
            }
            else{
                move(-1,0);
            }
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_D)){
            if(Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) && stamina != 0){
                move(1.5,0);
            }
            else{
                move(1,0);
            }
        }
    }
    public Texture getTexture(){
        return getTexture();
    }
    private void move(double magX, double magY){
        x += getSpeed() * magX;
        y += getSpeed() * magY;
    }
    private int getSpeed(){
        return stats.getPlayerSpeed();
    }
    @SuppressWarnings("unused")
    private int getLevel(){
        return stats.getLevel();
    }
    @SuppressWarnings("unused")
    private int getXP(){
        return stats.getXP();
    }
    public int getMaxHP(){
        return stats.getMaxHP();
    }
    public int getCurrentHealth(){
        return stats.getCurrentHealth();
    }
    public int getStrength(){
        return stats.getStrength();
    }
    public int getMagic(){
        return stats.getMagic();
    }
    public void addXP(int amt){
        stats.addXP(amt);
    }
    public void addHP(int amt){
        stats.addHP(amt);
    }
    public void addItem(Item item){
        inv.add(item);
    }
}

游戏类:

package geniushour.game;

import java.io.IOException;
import java.util.ArrayList;

import org.newdawn.slick.opengl.Texture;

import geniushour.gameobject.*;
import geniushour.gameobject.entity.*;
import geniushour.gameobject.item.*;

public class Game {

    private ArrayList<GameObject> obj;
    private ArrayList<GameObject> remove;
    private Player p;             private Texture pTexture;
    private ItemEssence essence;  private Texture eTexture;
    private EnemyBlob blob;       private Texture bTexture;

    public Game() throws IOException{
        obj = new ArrayList<GameObject>();
        remove = new ArrayList<GameObject>();
        setTexture(pTexture,"raptor");
        setTexture(eTexture,"essence");
        setTexture(bTexture,"blob");

        p = new Player(250,250,pTexture);
        essence = new ItemEssence(400, 400, eTexture, p);
        blob = new EnemyBlob(300,500,bTexture, 1);

        obj.add(essence);
        obj.add(p);
        obj.add(blob);
    }

    public void getInput(){
        p.getInput();
    }
    public void update(){
        for(GameObject go : obj){
            if(!go.getRemove()){
                go.update();
            }
            else{
                remove.add(go);
            }
        }
        for(GameObject go : remove){
            obj.remove(go);
        }
    }
    public void render(){
        for(GameObject go : obj){
            go.render();
        }
    }

    public ArrayList<GameObject> sphereCollide(float x, float y, float radius){
        ArrayList<GameObject> res = new ArrayList<GameObject>();
        for(GameObject go : obj){
            if(Util.dist(go.getX(),x,go.getY(),y) < radius){
                res.add(go);
            }
        }
        return res;
    }
    private void setTexture(Texture texture, String key){

    }
}

错误发生在Sprite类中,即第一个代码块。

0 个答案:

没有答案