AI.java と TestColl.java の 2 つのクラスがあります。
TestColl.java には AI.java と同じ問題があります
次のコード ブロックは AI.java からのものです。
public Vector2[] spawn = new Vector2[2];
public Vector2[] EnemPos = new Vector2[spawn.length];
public Vector2[] EnemVel = new Vector2[spawn.length];
public void PlaceEnemy(){
for(int i = 0; i < spawn.length;i++)//places enemies for the first time.
{
EnemPos[i] = new Vector2(0, 0);
EnemVel[i] = new Vector2(0, 0);
Gdx.app.log(log, "Creating EnemPos[" + Integer.toString(i) +"].x = " + Float.toString(EnemPos[i].x));
Gdx.app.log(log, "Creating EnemPos[" + Integer.toString(i) +"].y = " + Float.toString(EnemPos[i].y));
Gdx.app.log(log, "Creating EnemVel[" + Integer.toString(i) +"].x = " + Float.toString(EnemVel[i].x));
Gdx.app.log(log, "Creating EnemVel[" + Integer.toString(i) +"].y = " + Float.toString(EnemVel[i].y));
x = MathUtils.random(10,50);
y = MathUtils.random(10,50);
spawn[i] = new Vector2(x, y);
Gdx.app.log(log, "spawn["+ i + "], " + Float.toString(spawn[i].x) + ", " + Float.toString(spawn[i].y));
}
}
PlaceEnemy メソッドでは、ほとんどのベクトル変数に値が与えられています。しかし!それら、vector2 変数にアクセスすると、別のメソッドから null が返されます。
例えば。LogicChase() メソッドを呼び出して、敵に AI を適用します。
次のコード ブロックは、まだ AI.java クラス内にあります。
public void Decider(){
if(!decision){
// reason why it's 50/50 is that
// we will lower or raise the chance of having chasing = true
// when the time comes but for now it's only at 50/50
Random rand = new Random();
int Choose = rand.nextInt(100);
if(Choose <= 50){
chasing = true;
}
else if(Choose > 50){
chasing = false;
}
decision = true;
}
}
public float chooseRandomDirection(){
Random r = new Random();
float[] randDirection = new float[3];
randDirection[0] = 0;
randDirection[1] = 1;
randDirection[2] = -1;
int randChoice = r.nextInt(3);
return randDirection[randChoice];
}
public void setXDirection(float dir, int i){
EnemVel[i].x = 0;
Gdx.app.log(log, "Old EnemVel[" + Integer.toString(i) +"].x = " + Float.toString(EnemVel[i].x));
EnemVel[i].x = dir;
Gdx.app.log(log, "New EnemVel[" + Integer.toString(i) +"].x = " + Float.toString(EnemVel[i].x));
}
public void setYDirection(float dir, int i){
EnemVel[i].y = 0;
Gdx.app.log(log, "Old EnemVel[" + Integer.toString(i) +"].y = " + Float.toString(EnemVel[i].y));
EnemVel[i].y = dir;
Gdx.app.log(log, "New EnemVel[" + Integer.toString(i) +"].y = " + Float.toString(EnemVel[i].y));
}
public void move(int i){
spawn[i].x += (Gdx.graphics.getDeltaTime()* (velocity/2)) * EnemVel[i].x;
spawn[i].y += (Gdx.graphics.getDeltaTime()* (velocity/2)) * EnemVel[i].y;
}
public void logicChase()
{
for(int i=0; i<spawn.length; i++){
Gdx.app.log(log, "Old EnemVel[" + Integer.toString(i) +"].x = " + Float.toString(EnemVel[i].x));
Gdx.app.log(log, "Old EnemVel[" + Integer.toString(i) +"].y = " + Float.toString(EnemVel[i].y));
Gdx.app.log(log, "Old EnemPos[" + Integer.toString(i) +"].x = " + Float.toString(EnemPos[i].x));
Gdx.app.log(log, "Old EnemPos[" + Integer.toString(i) +"].y = " + Float.toString(EnemPos[i].y));
}
Decider();
if(!chasing){// if not in chase mode.
if(!moving){// if stationary.
if(!lock){// if directions has not yet to be set.
if(count <= 0){// if counter has yet to be set.
// SET DIRECTIONS FOR X AND Y;
for(int i = 0; i < spawn.length; i++){
EnemVel[i].y = 0;
Gdx.app.log(log, "Setting new EnemVel["+i+"] x and y");
setXDirection(chooseRandomDirection(),i);
setYDirection(chooseRandomDirection(),i);
Gdx.app.log(log, "New EnemVel["+i+"] x and y set");
}
// SET LOCK TO TRUE
lock = true;
// SET MOVING TO TRUE
moving = true;
// SET TIME LENGTH OF ENEMY MOVEMENT
count = 250;
}
}
}
else if(moving){// if enemy is on the move.
if(count <= 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
count = 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.
for(int i=0; i<spawn.length; i++){
move(i);
}
}
}
}
}
else if(chasing){
for(int i = 0; i < spawn.length; i++){
//can't access EnemPos[i].x;
EnemPos[i].x = 0;
EnemPos[i].y = 0;
Gdx.app.log(log, "Setting new EnemPos[" + i + "] x and y");
Gdx.app.log(log, "Old EnemPos[" + Integer.toString(i) +"].x = " + Float.toString(EnemPos[i].x));
EnemPos[i].x = Math.abs(spawn[i].x - tc.position.x);
Gdx.app.log(log, "New EnemPos[" + Integer.toString(i) +"].x = " + Float.toString(EnemPos[i].x));
Gdx.app.log(log, "Old EnemPos[" + Integer.toString(i) +"].y = " + Float.toString(EnemPos[i].y));
EnemPos[i].y = Math.abs(spawn[i].y - tc.position.y);
Gdx.app.log(log, "New EnemPos[" + Integer.toString(i) +"].y = " + Float.toString(EnemPos[i].y));
}
for(int i = 0; i < spawn.length; i++){
if(EnemPos[i].y > EnemPos[i].x){
EnemVel[i].y = velocity/2;
EnemVel[i].x = velocity/3;
}
if(EnemPos[i].x > EnemPos[i].y){
EnemVel[i].x = velocity/2;
EnemVel[i].y = velocity/3;
}
if(EnemPos[i].x == 0){
EnemVel[i].x = velocity;
EnemVel[i].y = 0;
}
if(EnemPos[i].y == 0){
EnemVel[i].x = velocity;
EnemVel[i].x = 0;
}
if(EnemPos[i].y == EnemPos[i].x){
EnemVel[i].y = velocity/3;
EnemVel[i].x = velocity/3;
}
if (spawn[i].x > tc.position.x)
{EnemVel[i].x*=-1;}
if (spawn[i].y > tc.position.y)
{EnemVel[i].y*=-1;}
}
if(count <= 0){
if (chasing){//if enemy is chasing
if(!lock){//lock is to check if counter has been set.
count = 200;
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.
count = 300;
lock = true;
decision = false;
}
else{
lock = false;
chasing = true;
}
}
}
}
}
LogicChase メソッド内から EnemPos[i].x または spawn[i].x にアクセスしようとするたびに、常に AI.logicChase(AI.java:218) または AI.logicChase で「java.lang.NullPointerException」を取得します。 (AI.java:142)
AI.java:218 のコードは
// **JAVA.LANG.NULL DETECTED AT THIS LINE OF CODE**
EnemPos[i].x = Math.abs(spawn[i].x - tc.position.x);
AI.java:142 のコードは
public void setXDirection(int dir, int i){
Gdx.app.log(log, "Old EnemPos[" + Integer.toString(i) +"].x = " + Float.toString(EnemPos[i].x));
EnemVel[i].x = dir;// **JAVA.LANG.NULL DETECTED AT THIS LINE OF CODE**
Gdx.app.log(log, "New EnemPos[" + Integer.toString(i) +"].x = " + Float.toString(EnemPos[i].x));
}
私の質問は簡単です。VECTOR2 変数から既に値を宣言しているのに、どうして NULL なのですか?!
また、AI.java と TestColl.java を確認したい人のために、ここにリンクがあります。 http://www.mediafire.com/view/t6sz5ifsokch1av/AI.java http://www.mediafire.com/view/6ssvky2zpuksbuk/TestColl.java
TestColl 実装をアプリケーション リスナーに置き換えるだけです。