我试图阅读并显示一个精灵,但我得到的只是这个错误:
Exception in thread "Thread-2" java.lang.NullPointerException
at Game.MainCharacter.render(MainCharacter.java:44)
at Game.Game.render(Game.java:105)
at Game.Game.run(Game.java:149)
at java.lang.Thread.run(Unknown Source)
精灵正确打开(我查了一下),但我不知道在哪里遗忘了......
以下是使用的类(我只编写了使用的代码): GlobalTextures类 - 包含我所有的spritesheeets
package Game;
import java.awt.image.BufferedImage;
public class GlobalTextures {
public BufferedImage player;
private SpriteSheet character;
public GlobalTextures(Game game) {
character = new SpriteSheet(game.getCharacterSpriteSheet());
getTextures();
}
private void getTextures() {
player = character.getSprite(1, 1, 333, 472);
if(player == null) System.out.println("It doesn't exist");
}
}
玩家等级 - 在那里我打开角色,我90%确定有错误,但我不知道为什么......
package Game;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class MainCharacter extends GameObject implements Entity{
public MainCharacter(double x, double y, GlobalTextures tex) {
super(x, y, tex);
}
public void render(Graphics g) {
Game game = new Game();
g.drawImage(tex.player, (int)x, (int) y, null);
}
}
精灵类 - 在这里我得到精灵并返回角色
package Game;
import java.awt.image.BufferedImage;
public class SpriteSheet {
private BufferedImage image;
public SpriteSheet(BufferedImage image)
{
this.image = image;
}
public BufferedImage getSprite(int col, int row, int width, int height)
{
BufferedImage img = image.getSubimage((col * 2000) - 2000, (row * 2829) - 2829, width, height);
return img;
}
}
/////////////////////////////////////////////// /////////////////////////////////
package Game;
public class GameObject {
protected double x;
protected double y;
protected GlobalTextures tex;
public GameObject(double x, double y, GlobalTextures tex){
this.x = x;
this.y = y;
this.tex = tex;
}
}