gpt4 book ai didi

java - LIBGDX:NPE 错误跟踪和实例化

转载 作者:行者123 更新时间:2023-12-01 04:18:59 25 4
gpt4 key购买 nike

我在程序中遇到了nullPointerException,但是正在使用的变量 vector 已被实例化并也在方法中设置。问题是我们尝试追根溯源,但仍然没有回答我们的问题为什么它会变成空。

代码:

static class AI {
private String log = AI.class.getSimpleName();
private boolean lock = false , placed = false , moving = false ,
chasing = true, drop = false, decision = false;
private float counter, x, y;
private int xDir, yDir;
private DugMan d;
private Enemy e;

public void Checker() {
if (chasing) {
e.ePosition.x += Gdx.graphics.getDeltaTime()*x;
e.ePosition.y += Gdx.graphics.getDeltaTime()*y;
}

if (moving) {Gdx.app.log(log, Integer.toString(xDir, yDir));}
if (chasing) {Gdx.app.log(log, Float.toString(counter));}
if (counter > 0) {counter--;}

if (e.ePosition.y > Gdx.graphics.getHeight() - 10) {
e.ePosition.y = Gdx.graphics.getHeight() - 10;
}
if (e.ePosition.y <= 0) {
e.ePosition.y = 1;
}
if (e.ePosition.x > Gdx.graphics.getWidth() - 10) {
e.ePosition.x = Gdx.graphics.getWidth() - 10;
}
if (e.ePosition.x <= 0) {
e.ePosition.x = 1;
}
}

public int chooseRandomDirection() {
Random r = new Random();
int[] randDirection = new int[3];
randDirection[0] = 0;
randDirection[1] = 1;
randDirection[2] = -1;
int randChoice = r.nextInt(3);
return randDirection[randChoice];
}

public void setXDirection(int dir) {
xDir = dir;
}

public void setYDirection(int dir) {
yDir = dir;
}

public void move() {
e.ePosition.x += (Gdx.graphics.getDeltaTime()* (e.eMax_vel/2)) * xDir;
e.ePosition.y += (Gdx.graphics.getDeltaTime()* (e.eMax_vel/2)) * yDir;
}

public void Decider() {
if (!decision) {

Random rand = new Random();
int Choose = rand.nextInt(100);

if (Choose <= 50) {
chasing = true;
}
else if (Choose > 50) {
chasing = false;
}
decision = true;
}
}


public void logicChase() {
Decider();
if (!chasing) {// if not in chase mode.
if (!moving) {// if stationary.
if (!lock) {// if directions has not yet to be set.
if (counter <= 0) {// if counter has yet to be set.
// SET DIRECTIONS FOR X AND Y;
setXDirection(chooseRandomDirection());
setYDirection(chooseRandomDirection());
// SET LOCK TO TRUE
lock = true;
// SET MOVING TO TRUE
moving = true;
// SET TIME LENGTH OF ENEMY MOVEMENT
counter = 250;
}
}
}

else if (moving) {// if enemy is on the move.
if (counter <= 0) {// if time counter for enemy movement has finished.
if (lock) {// if directions are in used.
// SET MOVING TO FALSE. ENEMY IS NOW RESTING
moving = false;
// SET NEW TIME COUNTER FOR ENEMY WHILE AT REST
counter = 500;
// SET LOCKS TO FALSE. DIRECTIONS ARE NOT IN USE.
lock = false;
// RANDOMIZE ENEMY MOVEMENT
decision = false;
}
}
else {// ENEMY IS STILL MOVING, ENEMY IS NOW MOVING.
if (lock) {// IF DIRECTIONS ARE STILL LOCKED.
move();
}
}
}
}

else if (chasing) {

float xdif = Math.abs(e.ePosition.x - d.dPosition.x); <<2nd trace error
float ydif = Math.abs(e.ePosition.y - d.dPosition.y);

if (ydif > xdif ){
y = e.eMax_vel;
x = e.eMax_vel/2;
}

if(xdif > ydif) {
x = e.eMax_vel;
y = e.eMax_vel/2;
}

if (xdif == 0) {
x = e.eMax_vel;
y = 0;
}

if (ydif == 0) {
y = e.eMax_vel;
x = 0;
}

if (ydif == xdif) {
y = e.eMax_vel/2;
x = e.eMax_vel/2;
}

if (e.ePosition.x > d.dPosition.x)
{x*=-1;}
if (e.ePosition.y > d.dPosition.y)
{y*=-1;}
if (counter <= 0) {
if (chasing) {//if enemy is chasing
if (!lock) {//lock is to check if counter has been set.
counter = 500;
lock = true;
}
else {
lock = false;
chasing = false;
}
}

else if (!chasing) {//if enemy stopped chasing
if (!lock) {//lock is to check if counter has been set.
counter = 250;
lock = true;
decision = false;
}
else {
lock = false;
chasing = true;
}
}
}
}
}

}

这是跟踪:

Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.teamkwan.dugmanalpha.level.TestLevel1$AI.logicChase(TestLevel1.java:552)
at com.teamkwan.dugmanalpha.level.TestLevel1.show(TestLevel1.java:908)

第 552 行是这一行:

float xdif = Math.abs(e.ePosition.x - d.dPosition.x);

并且eposition和dposition都是公开引用的,并且它们也在其他方法中实例化。

最佳答案

仔细检查您是否正在初始化变量 edePositiondPosition

关于java - LIBGDX:NPE 错误跟踪和实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19166185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com