这是我第一次在这里发帖。我一直在寻找解决方案但到目前为止没有运气。我对编码很新,所以我提前为可怕的编程失败道歉,无论如何我正在使用java和slick2d而且我正在编写游戏。
问题是,当我运行程序并尝试绘制敌人时,它会给我一个NullPointerException,我知道这与图像的路径无关,因为我已经尝试过与其他图像一起使用工作得很好。我认为问题是它不能识别图像值,但我已经尽力解决它并且到目前为止没有运气。我确信有一些非常简单的解决方案,而且我遇到了很多麻烦,所以我只是想知道是否有人可以帮助我。无论如何这里是代码。提前致谢。
package javagame;
import java.util.ArrayList;
import javax.swing.Timer;
import org.newdawn.slick.*;
import org.newdawn.slick.openal.Audio;
import org.newdawn.slick.openal.AudioLoader;
import org.newdawn.slick.state.*;
public class Play extends BasicGameState{
int time = 0;
int roomNo = 0;
Image interfaceBackground, spriteImage, background, enemyImage, projectileImageLeft, projectileImageRight, projectileImageUp, projectileImageDown, health0, health1, health2, health3, health4, health5, health6, health7, health8, health9, health10;
Audio projectileSound;
Sprite pChar = new Sprite(spriteImage, 400f, 400f);
Sprite enemy = new Enemy(enemyImage, 0f, 0f);
ArrayList<Enemy> enemyList = new ArrayList<Enemy>();
//Enemy skeleton = new Enemy(skeletonImage, 200, 200, 50, 50);
public Play(int state) {
}
@Override
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
projectileImageLeft = new Image("res/projectile2.png");
projectileImageRight = new Image("res/projectile3.png");
projectileImageUp = new Image("res/projectile1.png");
projectileImageDown = new Image("res/projectile4.png");
interfaceBackground = new Image("res/interfaceBackground.png");
health0 = new Image("res/HP0.png");
health1 = new Image("res/HP1.png");
health2 = new Image("res/HP2.png");
health3 = new Image("res/HP3.png");
health4 = new Image("res/HP4.png");
health5 = new Image("res/HP5.png");
health6 = new Image("res/HP6.png");
health7 = new Image("res/HP7.png");
health8 = new Image("res/HP8.png");
health9 = new Image("res/HP9.png");
health10 = new Image("res/HP10.png");
enemyImage = new Image("res/skeleton.png");
spriteImage = new Image("res/sprite2.png");
background = new Image("res/background.png");
background.setFilter(Image.FILTER_NEAREST);
spriteImage.setFilter(Image.FILTER_NEAREST);
projectileImageLeft.setFilter(Image.FILTER_NEAREST);
projectileImageRight.setFilter(Image.FILTER_NEAREST);
projectileImageUp.setFilter(Image.FILTER_NEAREST);
projectileImageDown.setFilter(Image.FILTER_NEAREST);
interfaceBackground.setFilter(Image.FILTER_NEAREST);
enemyImage.setFilter(Image.FILTER_NEAREST);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
Input input= gc.getInput();
if(input.isKeyDown(Input.KEY_W)){pChar.MoveUp(spriteImage);}
if(input.isKeyDown(Input.KEY_S)){pChar.MoveDown(spriteImage);}
if(input.isKeyDown(Input.KEY_A)){pChar.MoveLeft(spriteImage);}
if(input.isKeyDown(Input.KEY_D)){pChar.MoveRight(spriteImage);}
if(pChar.projectileCD > 200){
if(input.isKeyDown(Input.KEY_UP)){pChar.shootUp(projectileImageUp);}
else if(input.isKeyDown(Input.KEY_DOWN)){pChar.shootDown(projectileImageDown);}
else if(input.isKeyDown(Input.KEY_LEFT)){pChar.shootLeft(projectileImageLeft);}
else if(input.isKeyDown(Input.KEY_RIGHT)){pChar.shootRight(projectileImageRight);}
}
pChar.update();
enemy.update();
enemy.generate();
if(pChar.Health < 1){
sbg.enterState(3);
}
time += 1;
roomNo = pChar.roomNo;
for(Enemy enem: enemyList){
enem.update();
}
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
background.draw(0, 100, 800, 500);
spriteImage.draw(pChar.getxPos(), pChar.getyPos(), 120f, 80f);
pChar.draw();
enemy.draw();
interfaceBackground.draw(0,0,800,100);
g.setColor(Color.black);
g.drawString("Time:" + time/50, 50, 30);
g.drawString("Room Number:" +roomNo, 300, 30);
switch(pChar.Health){
case 0:
health0.draw(600, 20, 150, 65);
break;
case 1:
health1.draw(600, 20, 150, 65);
break;
case 2:
health2.draw(600, 20, 150, 65);
break;
case 3:
health3.draw(600, 20, 150, 65);
break;
case 4:
health4.draw(600, 20, 150, 65);
break;
case 5:
health5.draw(600, 20, 150, 65);
break;
case 6:
health6.draw(600, 20, 150, 65);
break;
case 7:
health7.draw(600, 20, 150, 65);
break;
case 8:
health8.draw(600, 20, 150, 65);
break;
case 9:
health9.draw(600, 20, 150, 65);
break;
case 10:
health10.draw(600, 20, 150, 65);
break;
}
}
@Override
public int getID(){
return 1;
}
}
//Sprite
package javagame;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.tests.xml.Entity;
public class Sprite{
ArrayList<Projectile> projectileList = new ArrayList<Projectile>();
ArrayList<Enemy> enemyList = new ArrayList<Enemy>();
protected static float xPos = 400f, yPos = 400f;
private static float Speed = 3.5f, ProjectileSpeed = 8f;
static int AttackSpeed = 6, Health = 10;
private int Damage = 0;
int projectileCD = 100, roomNo = 1;
Image SpriteImage, ProjectileImage, EnemyImage;
Random number = new Random();
public Sprite(Image spriteImage, float x, float y) {
SpriteImage = spriteImage;
}
public static float getSpeed(){
return Speed;
}
public static float getProjectileSpeed(){
return ProjectileSpeed;
}
public static int getHealth(){
return Health;
}
public static int getAttack(){
return AttackSpeed;
}
public int getDamage(){
return Damage;
}
public float getxPos(){
return xPos;
}
public float getyPos(){
return yPos;
}
public void MoveLeft(Image spriteImage){
if (xPos < 50){}
else{xPos -= Speed;}
}
public void MoveRight(Image spriteImage){
if(xPos > 670){}
else if(((yPos > 290)&&(yPos < 380))&&(xPos>669)){
roomNo++;
xPos += 150;
//next room
}
else{xPos += Speed;}
}
public void MoveUp(Image spriteImage){
if(yPos < 150){}
else{yPos -= Speed;}
}
public void MoveDown(Image spriteImage){
if(yPos > 420){}
else {yPos += Speed;}
}
public void shootUp(Image projectileImage){
projectileList.add(new Projectile(projectileImage, xPos + 15, yPos, 0f, -8f));
projectileCD = 0;
Health = Health -1;
}
public void shootDown(Image projectileImage){
projectileList.add(new Projectile(projectileImage, xPos + 35, yPos + 60, 0f, 8f));
projectileCD = 0;
}
public void shootLeft(Image projectileImage){
projectileList.add(new Projectile(projectileImage, xPos , yPos + 30, -8f, 0f));
projectileCD = 0;
}
public void shootRight(Image projectileImage){
projectileList.add(new Projectile(projectileImage, xPos + 60, yPos + 30, 8f, 0f));
projectileCD = 0;
}
public void draw(){
for(Projectile proj: projectileList){
proj.draw();
}
for(Enemy enem: enemyList){
enem.draw();
}
}
public void update(){
projectileCD = projectileCD + AttackSpeed;
for(Projectile proj: projectileList){
proj.move();
/*if(proj.xPos > 670){
projectileList.remove(this);
}
else if(proj.xPos < 80){
projectileList.remove(this);
}
else if(proj.yPos > 420){
projectileList.remove(this);
}
else if(proj.yPos < 80){
projectileList.remove(this);
}*/
}
}
public void generate(){
switch(number.nextInt(3)){
case 1:
enemyList.add(new Enemy(SpriteImage, 100f, 100f));
break;
case 2:
enemyList.add(new Enemy(ProjectileImage, 100f, 100f));
break;
case 3:
enemyList.add(new Enemy(SpriteImage, 100f, 100f));
break;
}
}
}
//Enemy
package javagame;
import java.util.Random;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
public class Enemy extends Sprite{
float xSpawn = 0, ySpawn = 0, Dx =0, Dy = 0, xPos = 0, yPos, Speed = 0;
int AttackSpeed = 0, Health = 0, Damage;
Image EnemyImage;
Random number = new Random();
public Enemy(Image enemyImage, float x, float y) {
super(enemyImage, x, y);
EnemyImage = enemyImage;
AttackSpeed = number.nextInt(6);
Damage = number.nextInt(3);
Health = number.nextInt(4);
Speed = number.nextInt(4);
}
public void draw(){
EnemyImage.draw(xPos, yPos); <---- This is where I get the error
}
}
答案 0 :(得分:0)
您尚未实例化yPos值。您将xPos设置为0但不设置为yPos,因此很有可能该值不存在。