简体   繁体   中英

(Java) Changing variables in a instance of a class

I've been look at this problem all day, and I can't seem to understand what is happening. Basically, I have 2 classes, one is a JFrame and the other is just a game loop really. The JFrame class is supposed to change some variables depending on what the user is clicking.

However, when clicking on the button, the methods don't change any variables. The only way I can change a variable in my JFrame is by calling a method in it, from my calling class.

Here is a sample of the relevant code.

public class CMBBattle {

public void startCombat (object.PlayerVariables p1Char1, object.PlayerVariables p1Char2, object.PlayerVariables p1Char3){
    boolean status;
    int teamMovesLeft = 0;

    Battle battle = new Battle(p1Char1, p1Char2, p1Char3);

    status = battle.combatStatus();
    teamMovesLeft = battle.getMovesLeft();
    while (status == true){
        teamMovesLeft = battle.getMovesLeft();
        if (teamMovesLeft <= 0){
            battle.createBattleOrder();
            battle.doBattle();
        }
        status = battle.combatStatus();
        if(status == true){
            battle.newRound();
        }
    }
  }

  public class Battle extends JFrame implements MouseListener {
     private String currentCharacter;

   private void characterOrders(String userChoice, String playerHit){
    int playerAttacker = 0;
    int abilityUsed = 0;
    int speedOfHit = 0;
    boolean finished = false;

    //TODO Currently nothing is happening with what you actually used, i think this was in the old code. Re-implement
    //TODO At this point I am going to just say everything is a quick attack.  So, it'll take up 1 per attack.
    //TODO Add logic to see if something is over their move limit or not
    speedOfHit = 1;

    if(currentCharacter.equals(Player1.charName)){
        playerAttacker = 1;
        char1Orders[moveUses][0] = figureDamage(abilityUsed);
        char1Orders[moveUses][1] = playerAttacker;
        char1Orders[moveUses][2] = getCharacterID(playerHit);
        if((currentPlayerMoves - speedOfHit) <= 0){
            player1.ordersFinished = true;
            finished = true;
        }
    }
    else if(currentCharacter.equals(player2.charName)){
        playerAttacker = 2;
        char2Orders[moveUses][0] = figureDamage(abilityUsed);
        char2Orders[moveUses][1] = playerAttacker;
        char2Orders[moveUses][2] = getCharacterID(playerHit);
        if((currentPlayerMoves - speedOfHit) <= 0){
            player2.ordersFinished = true;
            finished = true;
        }
    }
    else if(currentCharacter.equals(player3.charName)){
        playerAttacker = 3;
        char3Orders[moveUses][0] = figureDamage(abilityUsed);
        char3Orders[moveUses][1] = playerAttacker;
        char3Orders[moveUses][2] = getCharacterID(playerHit);
        if((currentPlayerMoves - speedOfHit) <= 0){
            player3.ordersFinished = true;
            finished = true;
        }
    }
    moveUses += 1;

    //The following decides if it's time for the next player or not, if this is the last player,
    //then it's time to set it to zero and let the handler do the rest.
    if(finished == true){
        if(player1.ordersFinished == false){
            currentCharacter = player1.charName;
            currentPlayerMoves = player1.moves;
        }
        else if(player2.ordersFinished == false){
            currentCharacter = player2.charName;
                            //THE PROBLEM IS RIGHT HERE, THE LINE ABOVE SHOULD HAVE
                            //CHANGED CURRENTCHARACTER, BUT IT DID NOT
            currentPlayerMoves = player2.Moves;
        }
        else if(player3.ordersFinished == false){
            currentCharacter = player3.charName;
            currentPlayerMoves = player3.moves;
        }
        else {
            currentCharacter = "";
            currentPlayerMoves = 0;
        }
        moveUses = 0;
    }
    else{
        currentPlayerMoves -= 1;
    }

    /*
    if (actualUses != Character.moves){
        //TODO We should add logic so that an unitilized variable isn't used...
        //If they don't do anything, set the rest of their array to zeroe's so we can later say, if zero exclude them from round or don't worry about the shite
    }
    */
}

   public String setCurrent(){
   currentCharacter = "NewPerson";
    }

    @Override
public void mouseClicked(MouseEvent e) {
    if(!currentCharacter.equals("")){
        String playerHit = JOptionPane.showInputDialog(null, "Who should "                          + currentCharacter + " attack?");
        if(e.getSource() == attackButton){
            characterOrders("Attack", playerHit);
        }
    }
}

}

Now if I call setCurrent from my startCombat method, the variable changes. However, clicking does not change the variable. It will run the code fine, however when it gets to the part where it changes it, it will not change it. I'm sure I'm just missing something, but I can't figure out the rule that I've over looked...

--UPDATE-- I updated the CharacterOrders to reflect my actual code as it is right now Please note that currentCharacter is the current Character who is issuing orders.

shouldn't it be

private void CharacterOrders(String userChoice, String PlayerHit)
{
    currentCharacter = PlayerHit;
}

Okay, since the code is incomplete and I'm finding it somewhat difficult to follow, I could be totally on the wrong track here.

But...where did playerHit come from in your listener for mouseClicked? Where is it being set? I'm not sure, but maybe playerHit doesn't correspond to the name of any of the players, and as a result, it's not entering your first set of three if-else statements, and so finished is never true ?

If you can post more information, or maybe make the code more informative, we might be able to get more of an idea as to what your problem is. Sorry if this wasn't the solution.

but it might make the code a bit cleaner: in your first set of if-else statements in characterOrders , maybe you could just set some variable to be either char1Orders , char2Orders or char3Orders instead of having to call each one separately? 但是它可能会使代码更清晰:在characterOrders第一组if-else语句中,也许您可​​以将一些变量设置为char1Orderschar2Orderschar3Orders而不必分别调用每个变量? Then operate on the variable instead?

So kinda like

if(currentCharacter.equals(Player1.charName)) {
    myVariable = char1Orders;
}
else if(/*...a condition...*/) {
   //...more code
}
//...
myVariable[0] = /*...*/;
//...etc.

The problem is my lack of understand of threads (which I have now rectified). Since i was creating my JFrame, and then hoping it would just run concurrently with the code, it was never allowed to execute it's code because technically it was still being created. So, pay attention to threads is the answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM