[英]Java Checking a player ID
currently working on a project and I have a player class where I have an ID that auto-increments every time an object is created and then all my objects are in an array list (PlayerList).I am trying to set the text of a label to the player name but it just skips the first player and then it won't loop back round I get a java runtime error.目前正在处理一个项目,我有一个播放器类,其中我有一个 ID,每次创建对象时都会自动递增,然后我的所有对象都在数组列表 (PlayerList) 中。我正在尝试设置标签的文本到播放器名称,但它只是跳过第一个播放器,然后它不会循环返回我收到 Java 运行时错误。
This code loads all the players into the table:此代码将所有玩家加载到表中:
public void initialize() {
for (RadioButton button : radios) {
button.setDisable(true);
button.setOpacity(1);
}
loadPlayers();
selectPlayer(0);
}
public void selectPlayer(int count){
for(Player player : PlayerList) {
if(player.getPlayerID() == count) {
Player activePlayer = PlayerList.get(count);
playerName.setVisible(true);
String name = activePlayer.getName();
playerName.setText(name);
System.out.println(count);
System.out.println(activePlayer);
}
}
}
From the code above I want to cycle through the list of players and pull the player from a count, I also have the code below to run on a button press, this code below adds to the count or resets the count.从上面的代码中,我想循环播放玩家列表并从计数中拉出玩家,我还有下面的代码可以在按下按钮时运行,下面的代码会增加计数或重置计数。
if(count == PlayerList.size()){
count = 0; //reset the count
}else{
count++;
selectPlayer(count);
}
When I'm running the code it always pulls the second player rather than the first one I'm not sure what is going wrong, any help will be very much appreciated.当我运行代码时,它总是拉动第二个玩家而不是第一个我不确定出了什么问题,非常感谢任何帮助。
Updated更新
if (count <= size) {
Player activePlayer = PlayerList.get(count);
playerName.setVisible(true);
String name = activePlayer.getName();
playerName.setText(name);
System.out.println(count);
}else if(count == size){
count=0;
selectPlayer(count);
}
This is what I have got at the minute, it works but when I come to the last player I have to press the button twice to loop round to the start?这就是我现在得到的,它有效,但是当我来到最后一个玩家时,我必须按两次按钮才能循环到开始?
It's taking the second player cause you increment count
first.它需要第二个玩家,因为您首先增加
count
。 The order is this:顺序是这样的:
selectPlayer(count);
count++;
I think that when you call the button press code the first time (assuming count == 0), it is falling into the "else" condition.我认为当您第一次调用按钮按下代码时(假设计数 == 0),它会陷入“其他”条件。 In the "else" block, the first thing you do is increment count (so now, count == 1).
在“else”块中,您要做的第一件事是增加计数(现在,计数 == 1)。 Your first player probably has ID=0, and your second player has ID=1.
您的第一个玩家的 ID 可能为 0,而您的第二个玩家的 ID 为 1。 To fix, switch the two lines in your else block so you select the player THEN increment count.
要修复,请切换 else 块中的两行,以便选择玩家 THEN increment count。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.