Java 阵列列表在战斗逻辑中似乎不能正常工作?

Java 阵列列表在战斗逻辑中似乎不能正常工作?,java,android,Java,Android,我正在为Android做一个塔楼进攻游戏,我的战斗系统中出现了一些不寻常的行为。我有一个仆从穿过屏幕,与敌军部队相撞。他们参加战斗,但由于某种原因,敌人退出了战斗 生成单位后,它们将存储在阵列列表中。我使用了一个lockedOn变量,它在数组列表中存储一个指向当前正在使用的对象的指针。似乎当一个新的敌人单位诞生时,它会对当前战斗中的敌人产生负面影响。我不知道当一个新项目被添加到仆从锁定的数组列表中时,是否有什么东西在改变对象的指针。我已经尝试了我能想到的一切,但我不能缩小问题的范围 我发布的代码

我正在为Android做一个塔楼进攻游戏,我的战斗系统中出现了一些不寻常的行为。我有一个仆从穿过屏幕,与敌军部队相撞。他们参加战斗,但由于某种原因,敌人退出了战斗

生成单位后,它们将存储在阵列列表中。我使用了一个lockedOn变量,它在数组列表中存储一个指向当前正在使用的对象的指针。似乎当一个新的敌人单位诞生时,它会对当前战斗中的敌人产生负面影响。我不知道当一个新项目被添加到仆从锁定的数组列表中时,是否有什么东西在改变对象的指针。我已经尝试了我能想到的一切,但我不能缩小问题的范围

我发布的代码中删除了很多额外的内容,以便于查看。我包括主游戏类和战士类的副本。冠军等级与战士相同,攻击和攻击速度值略有不同

主要游戏类

public final List<Minion_Warrior> mWarriors;
public final List<Enemy_Champion> eChampions;

this.mWarriors = new ArrayList<Minion_Warrior>();
this.eChampions = new ArrayList<Enemy_Champion>();

public void update(float deltaTime) {

    removeDead();
    updateMinions(deltaTime);
    updateEnemies(deltaTime);
    updateTowers(deltaTime);
}

private void removeDead() {
    int len = mWarriors.size();
    for(int i = 0; i < len; i++) {
        Minion_Warrior warrior = mWarriors.get(i);
        if(warrior.canDie) {
            mWarriors.remove(warrior);
            warrior = null;
            len = mWarriors.size();
        }
    }
    len = eChampions.size();
    for(int i = 0; i < len; i++) {
        Enemy_Champion champion = eChampions.get(i);
        if(champion.canDie) {
            eChampions.remove(champion);
            champion = null;
            len = eChampions.size();
        }
    }
}

private void updateMinions(float deltaTime) {

    updateWarriors(deltaTime);
}

private void updateWarriors(float deltaTime) {
    // For each warrior
    int lenWar = mWarriors.size();
    for(int i = 0; i < lenWar; i++) {
        Minion_Warrior warrior = mWarriors.get(i);

        // If the warrior is alive
        if(warrior.isAlive) {

            // If the warrior has no current target
            if(warrior.lockedOn == null) {

                // Check each champion
                int lenChamp = eChampions.size();
                for(int j = 0; j < lenChamp; j++) {
                    Enemy_Champion champion = eChampions.get(j);

                    // If a champion is alive
                    if(champion.isAlive) {

                        // And the champion does not currently have a target
                        if(champion.lockedOn == null) {

                            // Has the champion collided with the warrior?
                            if(OverlapTester.overlapRectangles(champion.bounds, warrior.bounds)) {
                                warrior.lockedOn = champion;
                                champion.lockedOn = warrior;
                                warrior.state = warrior.MINION_STATE_COMBAT;    
                                champion.state = champion.ENEMY_STATE_COMBAT;
                                break;
                            }
                        }
                    }   
                }
            }
        }

        warrior.update(deltaTime);
    }
}

private void updateEnemies(float deltaTime) {   
    updateChampions(deltaTime);
}

private void updateChampions(float deltaTime) {
    int len = eChampions.size();
    for(int i = 0; i < len; i++) {
        Enemy_Champion champion = eChampions.get(i);
        champion.update(deltaTime);
    }
}

private void updateTowers(float deltaTime) {
    if(spawnEnemies) {          
        if(!tCastleCanDeploy) {
            tCastleTimer += deltaTime;
            if(tCastleTimer > 10) {
                tCastleTimer = 0;
                tCastleCanDeploy = true;
                tCastleQue = 3;
            }
        }
        else if(tCastleCanDeploy && !tCastleWaiting) {
            tCastleDeployTimer += deltaTime;
            if(tCastleDeployTimer > 2) {
                tCastleDeployTimer = 0;
                tCastleWaiting = true;
            }
        }
        else if(tCastleCanDeploy && tCastleWaiting) {
            if(tCastleQue == 3) {
                Enemy_Champion champion = new Enemy_Champion(145, 51 - 0.5f);
                eChampions.add(champion);
                tCastleQue -= 1;
                tCastleWaiting = false;
            }
            else if(tCastleQue == 2) {
                Enemy_Archer archer = new Enemy_Archer(145, 51 - 0.5f);
                eArchers.add(archer);
                tCastleQue -= 1;
                tCastleWaiting = false;
            }
            else if(tCastleQue == 1) {
                Enemy_Mage mage = new Enemy_Mage(145, 51 - 0.5f);
                eMages.add(mage);
                tCastleQue -= 1;
                tCastleWaiting = false;
            }
            else if(tCastleQue == 0) {
                tCastleWaiting = false;
                tCastleCanDeploy = false;
            }
        }
    }
}  
公开最终名单;
公开最终名单;
this.mWarriors=新的ArrayList();
this.eChampions=newArrayList();
公共无效更新(浮动增量时间){
除去头();
更新(deltaTime);
更新时间(deltaTime);
更新服务器(deltaTime);
}
私有无效删除头(){
int len=mWarriors.size();
对于(int i=0;i10){
tCastleTimer=0;
tCastleCanDeploy=true;
tCastleQue=3;
}
}
else if(tCastleCanDeploy&!tCastleWaiting){
tCastleDeployTimer+=延迟时间;
如果(tCastleDeployTimer>2){
tCastleDeployTimer=0;
tCastWaiting=真;
}
}
else if(tCastleCanDeploy&tCastleWaiting){
如果(tCastleQue==3){
敌方冠军=新敌方冠军(145,51-0.5f);
添加(冠军);
tCastleQue-=1;
tCastleWaiting=假;
}
else if(tCastleQue==2){
敌方弓箭手=新敌方弓箭手(145,51-0.5f);
增加(弓箭手);
tCastleQue-=1;
tCastleWaiting=假;
}
else if(tCastleQue==1){
敌方法师=新敌方法师(145,51-0.5f);
添加(法师);
tCastleQue-=1;
tCastleWaiting=假;
}
else if(tCastleQue==0){
tCastleWaiting=假;
tCastleCanDeploy=false;
}
}
}
}  
仆从战士级

public class Minion_Warrior extends DynamicGameObject {
    public static final float MINION_WIDTH = 4.0f;
    public static final float MINION_HEIGHT = 4.0f;
    public static final int MINION_STATE_DEPLOY = 0;
    public static final int MINION_STATE_ROUTE = 1;
    public static final int MINION_STATE_COMBAT = 2;
    public static final int MINION_STATE_DEATH = 3;
    public static final float MINION_VELOCITY = 1.75f;

    public enum Animation {STANDING, WALKING, ATTACKING, DYING}

    int life;
    int attack;
    float attackSpeed;
    float attackTimer;
    boolean canAttack;
    float deathTimer;
    boolean canDie;
    boolean isAlive;
    int state;
    int cost;
    float stateTime;
    public static Enemy_Champion lockedOn;
    Animation animation = Animation.WALKING;
    Rectangle collisionBox;

    public Minion_Warrior(float x, float y) {
        super(x, y, MINION_WIDTH, MINION_HEIGHT);
        life = 100;
        attack = 15;
        attackSpeed = 1.8f;
        attackTimer = 0;
        canAttack = true;
        deathTimer = 0;
        canDie = false;
        isAlive = true;
        state = MINION_STATE_DEPLOY;
        cost = 0;
        stateTime = 0;
        velocity.set(MINION_VELOCITY, 0);
        lockedOn = null;
    }

    public void update(float deltaTime) {
        // Adjust the collision detection box
        //collisionBox = new Rectangle(position.x + 1, position.y - 2, 1.1f, 1);
        //bounds = collisionBox;
        bounds.lowerLeft.set(position).sub(MINION_WIDTH / 2, MINION_HEIGHT / 2);

        switch(state) {

        case MINION_STATE_DEPLOY:
            // Move to lane
            if(position.x < 35 && position.y > World.LANE_1 + 1.5) {
                velocity.set(MINION_VELOCITY, -MINION_VELOCITY);
                position.add(velocity.x * deltaTime, velocity.y * deltaTime);
            } else {
                velocity.set(MINION_VELOCITY, 0);
                position.add(velocity.x * deltaTime, 0);
                state = MINION_STATE_ROUTE;
            }
            break;

        case MINION_STATE_ROUTE:
            canAttack = true;
            position.add(velocity.x * deltaTime, 0);
            break;

        case MINION_STATE_COMBAT:
            // If there is a target
            if(lockedOn.isAlive) {
                // If the minion can not attack
                if(!canAttack) {
                    attackTimer += deltaTime;
                    if(attackTimer > attackSpeed) {
                        attackTimer = 0;
                        canAttack = true;
                    }
                }
                // If the minion can attack
                else if(canAttack) {
                    animation = animation.ATTACKING;
                    canAttack = false;
                    lockedOn.life -= attack;
                }
                checkOpponent();
            } else {
                state = MINION_STATE_ROUTE;
            }
            break;

        case MINION_STATE_DEATH:
            animation = animation.DYING;
            if(!canDie) {
                deathTimer += deltaTime;
                if(deathTimer > 2) {
                    deathTimer = 0;
                    canDie = true;
                }
            }
        }

        stateTime += deltaTime;
    }

    private void checkOpponent() {
        if(lockedOn.life <= 0) {
            lockedOn.state = lockedOn.ENEMY_STATE_DEATH;
            lockedOn.isAlive = false;
            lockedOn.lockedOn = null;

            state = MINION_STATE_ROUTE; 
            animation = animation.WALKING;
            lockedOn = null;
        }
    }
}
public类Minion\u Warrior扩展DynamicGameObject{
公共静态最终浮动仆从宽度=4.0f;
公共静态最终浮子仆从高度=4.0f;
公共静态final int MINION\u STATE\u DEPLOY=0;
公共静态最终int MINION\u STATE\u ROUTE=1;
公共静态最终整数仆从状态战斗=2;
公共静态最终整数仆从状态死亡=3;
公共静态最终浮子仆从速度=1.75f;
公共枚举动画{站立、行走、攻击、死亡}
智力生活;
智力攻击;
浮动攻击速度;
浮动攻击定时器;
布尔攻击;
浮动死亡计时器;
布尔坎迪;
我是活的;
int状态;
国际成本;
弗洛亚