(Java)更改类实例中的变量

时间:2012-01-09 00:23:23

标签: java class variables jframe

我整天都在看这个问题,我似乎无法理解发生了什么。基本上,我有2个类,一个是JFrame,另一个只是一个游戏循环。 JFrame类应该根据用户点击的内容更改一些变量。

但是,单击按钮时,方法不会更改任何变量。我可以在JFrame中更改变量的唯一方法是从我的调用类中调用一个方法。

以下是相关代码的示例。

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);
        }
    }
}

}

现在,如果我从setCurrent方法调用startCombat,则变量会发生变化。但是,单击不会更改变量。它将运行代码很好,但是当它到达它改变它的部分时,它不会改变它。我确定我只是遗漏了一些东西,但我无法弄清楚我已经看过的规则...

- 更新 - 我更新了CharacterOrders以反映我现在的实际代码 请注意,currentCharacter是发出订单的当前Character

3 个答案:

答案 0 :(得分:0)

不应该是

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

答案 1 :(得分:0)

好的,由于代码不完整而我发现有些难以理解,我可能完全走错了路。

但是... playerHit来自你的mouseClicked听众?它在哪里设置?我不确定,但也许playerHit与任何玩家的名字不对应,因此,它不会输入您的第一组三个if-else语句,因此{{ 1}}永远不会finished

如果您可以发布更多信息,或者可以使代码更具信息性,我们可以更好地了解您的问题是什么。对不起,如果这不是解决方案。

不是解决方案的一部分但它可能会使代码更清晰:在true的第一组if-else语句中,也许你可以设置一些变量是characterOrderschar1Orders还是char2Orders,而不必分别拨打每个人?然后对变量进行操作?

有点像

char3Orders

答案 2 :(得分:0)

问题是我对线程缺乏了解(我现在已经纠正过了)。因为我正在创建我的JFrame,然后希望它只是与代码同时运行,所以从未允许它执行它的代码,因为从技术上讲它仍然是被创建的。所以,注意线程就是答案。