我在目前正在努力制作的2D游戏中遇到碰撞检测问题。出于某种原因,我继续收到以下错误:
线程“Thread-2”中的异常java.lang.NullPointerException
at Objects.Bullet.Collision(Bullet.java:57)
at Objects.Bullet.tick(Bullet.java:39)
在window.Handler.tick(Handler.java:15)
在window.Game.tick(Game.java:96)
在window.Game.run(Game.java:76)
at java.lang.Thread.run(Unknown Source)
这里是我目前为我的子弹提供的代码:
package Objects;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.LinkedList;
import window.Game;
import window.Handler;
import Framework.GameObject;
import Framework.ObjectId;
import Framework.Texture;
public class Bullet extends GameObject{
private float width=32,height=32;
private final float MAX_SPEED = 10;
private Handler handler;
Texture tex= Game.getInstance();
public Bullet(float x,float y, ObjectId id,int velX){
super(x,y,id);
this.velX = velX;
}
public void tick(LinkedList<GameObject> object) {
x+=velX;
y+=velY;
if(velX<0)orientation =-1;
else if(velX>0)orientation=1;
if(velY>MAX_SPEED){
velY = MAX_SPEED;}
Collision(object);
}
public void render(Graphics g){
g.drawImage(tex.bullet[0],(int)x ,(int)y,null);
}
public Rectangle getBoundsRight() {
return new Rectangle((int) ((int)x+width-5),(int)y+5,(int)5, (int)height-10);
}
public Rectangle getBoundsLeft() {
return new Rectangle((int)x,(int)y+5,(int)5,(int)height-10);
}
private void Collision(LinkedList<GameObject>object){
for(int i=0; i<handler.object.size();i++){
GameObject tempObject = handler.object.get(i);
if(tempObject.getId() == ObjectId.Block){
if(getBoundsRight().intersects(tempObject.getBounds())){
velX=0;
x = tempObject.getX()- width;
}
if(getBoundsLeft().intersects(tempObject.getBounds())){
velX = 0;
x = tempObject.getX()+ (30);
}
}
}
}
public Rectangle getBounds() {
return null;
}
}
我基本上复制了我的播放器类中的代码,其中一切都运行良好但由于某种原因我的子弹类错误
我的播放器类的代码如下所示:
package Objects;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.LinkedList;
import window.Animation;
import window.Game;
import window.Handler;
import Framework.GameObject;
import Framework.ObjectId;
import Framework.Texture;
public class Player extends GameObject{
private float width = 64, height = 64;
private float gravity = 0.5f;
private final float MAX_SPEED = 10;
private Handler handler;
Texture tex = Game.getInstance();
//1= right
//-1= left
private Animation playerWalk,playerWalkLeft;
public Player(float x, float y,Handler handler, ObjectId id) {
super(x, y, id);
this.handler = handler;
playerWalk = new Animation(5,tex.player[1],tex.player[2],tex.player[3],tex.player[4],tex.player[5],tex.player[6]);
playerWalkLeft = new Animation(5,tex.player[7],tex.player[8],tex.player[9],tex.player[10],tex.player[11],tex.player[12]);
}
public void tick(LinkedList<GameObject> object) {
x+=velX;
y+=velY;
if(velX<0)orientation =-1;
else if(velX>0)orientation=1;
if(falling||jumping){
velY+=gravity;
if(velY>MAX_SPEED){
velY = MAX_SPEED;
}
}
Collision(object);
playerWalk.runAnimation();
playerWalkLeft.runAnimation();
}
private void Collision(LinkedList<GameObject>object){
for(int i=0; i<handler.object.size();i++){
GameObject tempObject = handler.object.get(i);
if(tempObject.getId() == ObjectId.Block){
if(getBoundsTop().intersects(tempObject.getBounds())){
y= tempObject.getY()+40 ;
velY=0;
}
if(getBounds().intersects(tempObject.getBounds())){
velY=0;
y= tempObject.getY()-height;
falling = false;
jumping = false;
}else
falling = true;
//Right
if(getBoundsRight().intersects(tempObject.getBounds())){
x = tempObject.getX()- width;
}
//Left
if(getBoundsLeft().intersects(tempObject.getBounds())){
x = tempObject.getX()+ (30);
}
}
}
}
public void render(Graphics g) {
g.setColor(Color.BLUE);
if(velX != 0 ){
if(orientation == 1)
playerWalk.drawAnimation(g,(int)x,(int)y,64,64);
else
playerWalkLeft.drawAnimation(g,(int)x,(int)y,64,64);
}else
if(orientation == 1)
g.drawImage(tex.player[1],(int)x,(int)y,64,64,null); //change here to change the size of the ball. does not account for hit-box must change in the width and height global variables above
else if(orientation == -1)
g.drawImage(tex.player[12],(int)x,(int)y,64,64,null);
}
public Rectangle getBounds() {
return new Rectangle((int) ((int)x+(width/2)-((width/2)/2)),(int) ((int)y+(height/2)),(int)width/2,(int)height/2);
}
public Rectangle getBoundsTop() {
return new Rectangle((int) ((int)x+(width/2)-((width/2)/2)),(int)y,(int)width/2,(int)height/2);
}
public Rectangle getBoundsRight() {
return new Rectangle((int) ((int)x+width-5),(int)y+5,(int)5,(int)height-10);
}
public Rectangle getBoundsLeft() {
return new Rectangle((int)x,(int)y+5,(int)5,(int)height-10);
}
}
任何形式的帮助/提示或建议将不胜感激