-2

我有 2 个类,AI.java 和 TestColl.java。

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 变量中声明了值时,为什么它是空的?!

对于那些想要检查 AI.java 和 TestColl.java 的人来说,这里是链接。 http://www.mediafire.com/view/t6sz5ifsokch1av/AI.java http://www.mediafire.com/view/6ssvky2zpuksbuk/TestColl.java

只需用应用程序侦听器替换 TestColl 实现。

4

2 回答 2

0

您还没有将元素放入数组中EnemPos。尝试删除该行中的注释

EnemPos[i] = new Vector2(x, y);
于 2013-10-03T01:36:49.317 回答
0

您发布的代码均已注释。即:我看不到从哪里setXDirection调用该方法(全部注释掉),但我猜是您在调用之前调用了该方法public void PlaceEnemy(),在那里您初始化了数组中的向量。请仔细检查。(如果您可以发布正确的代码,那就太好了)

于 2013-10-03T16:41:02.233 回答