我最近一直在LWJGL工作,我遇到了一个问题。我创建了一个inflictDamage()方法,但是当我激活它时它返回一个Null Pointer Exception。这是我的代码: inflictDamage()方法:
public static void inflictDamage(int type, GameObject attacker, GameObject enemy, boolean facingLeft, float damageIncrement, float ehp, float attackerCenter, float enemyCenter){
float attackX = attackerCenter - 16;
if(type == 1){
if(facingLeft == true){
attackX = attackerCenter - 16;
if(attackX == enemyCenter){
ehp -= damageIncrement;
}
}
else if(facingLeft == false){
attackX = attackerCenter + 16;
if(attackX == enemyCenter){
ehp -= damageIncrement;
}
}
}
}
激活的地方:
package geniushour.game;
import org.lwjgl.input.Keyboard;
public class GOMainCharacter extends GameObject{
public static final int SIZE = 50;
public static final float MaxSpeedX = 4f;
public static final float MaxSpeedY = 8f;
public float velX;
public float velY;
public final float baseOffsetX;
public final float baseOffsetY;
public static float telepointX;
public static float telepointY;
public int cType = 1;
public static boolean faceLeft = true;
public static GOMainCharacter p;
public static GOMonster m;
public GOMainCharacter(float offsetX, float offsetY){
this.offsetX = offsetX;
this.offsetY = offsetY;
this.yPos = SIZE;
this.xPos = SIZE;
this.baseOffsetX = this.offsetX;
this.baseOffsetY = this.offsetY;
velX = -MaxSpeedX;
velY = -MaxSpeedY;
}
@Override
void update() {
if(Keyboard.isKeyDown(Keyboard.KEY_A)){
telepointX = offsetX;
telepointY = baseOffsetY;
faceLeft = true;
offsetX += velX;
}
if(Keyboard.isKeyDown(Keyboard.KEY_D)){
telepointX = offsetX;
telepointY = baseOffsetY;
faceLeft = false;
offsetX += -velX;
}
if(Keyboard.isKeyDown(Keyboard.KEY_S)){
Draw.inflictDamage(cType, p, m, faceLeft, 10, m.healthPoints, p.getCenterY(), m.getCenterY());
}
}
}
我还可以提供GameObject类,如果需要的话。