jeudi 10 janvier 2019

Why does this if statement not behave properly

I'm trying to make it so that enemies in a game I'm working on take damage if the attack enters the same panel as the enemy. Both are represented by a Point (atkpnlpos and pnlpos) within their respective classes. The enemies are passed in as an array and I use a for loop to check each of them.

//Check if a hit connects
    boolean hit = false;
    for (int index=0;index<3;index++){

        if(bsenemylist[index]!=null){
            //TODO figure out if the list is filled
            //bsenemylist[index].takeDmg(atkdmg);

            if(atkpnlpos.equals(bsenemylist[index].pnlpos)==true){
                bsenemylist[index].takeDmg(atkdmg);
                hit = true;
            }
        }
    }

    //end attack if any hit connects
    if (hit==true){
        endAttack();
    }

I know that the bsenemylist is not null because the commented out code works properly and drops the HP of all the enemies for every tick the attack is over panelX,Y if I change the innermost if loop to

if(atkpnlpos.equals(new Point(X,Y))==true){
                bsenemylist[index].takeDmg(atkdmg);
                hit = true;
            }

I made each enemy show their respective pnlpos in a textview, and it works correctly when I make X and Y equal pnlpos.x and pnlpos.y

Aucun commentaire:

Enregistrer un commentaire