因此,我尝试了所有想到的事情。 在Google中进行了研究-实施了类似问题的解决方案。
我有一个布尔值“ ruch”,应该在每次单击鼠标时在播放器之间切换。可悲的是,在切换到第二个播放器后,它再也没有切换回第一个播放器。 我是java和stackoverflow的新手,所以我非常感谢您的帮助。
boolean ruch;
@FXML
private void onCellClick(MouseEvent e) {
Node source = (Node) e.getSource();
Integer colIndex = GridPane.getColumnIndex(source);
Integer rowIndex = GridPane.getRowIndex(source);
TranslateTransition transition = new TranslateTransition();
transition.setDuration(Duration.seconds(1));
if (!ruch) {
ruch = true;
transition.setNode(group);
transition.setToX((colIndex.intValue() * 100));
transition.setToY((rowIndex.intValue() * 100));
animation = transition;
animation.play();
} else {
ruch = false;
transition.setNode(group2);
if (colIndex.intValue() == 3 && rowIndex.intValue() == 3) {
transition.setToX(200);
transition.setToY(200);
} else if (colIndex.intValue() == 4 && rowIndex.intValue() == 5) {
transition.setToX(300);
transition.setToY(200);
} else {
transition.setToX((colIndex.intValue() * 100));
transition.setToY((rowIndex.intValue() * 100));
}
animation = transition;
animation.play();
}
ruch = false;
}
答案 0 :(得分:0)
之后始终设置组2(player2) 将transition.setNode(group2)放在else块中。
private void onCellClick(MouseEvent e) {
Node source = (Node) e.getSource();
Integer colIndex = GridPane.getColumnIndex(source);
Integer rowIndex = GridPane.getRowIndex(source);
TranslateTransition transition = new TranslateTransition();
transition.setDuration(Duration.seconds(1));
if (!ruch) {
ruch = true;
transition.setNode(group);
transition.setToX((colIndex.intValue() * 100));
transition.setToY((rowIndex.intValue() * 100));
animation = transition;
animation.play();
} else{
ruch = false;
transition.setNode(group2);
if (colIndex.intValue() == 3 && rowIndex.intValue() == 3) {
transition.setToX(200);
transition.setToY(200);
} else if (colIndex.intValue() == 4 && rowIndex.intValue() == 5) {
transition.setToX(300);
transition.setToY(200);
} else {
transition.setToX((colIndex.intValue() * 100));
transition.setToY((rowIndex.intValue() * 100));
}
animation = transition;
animation.play();
}
}
答案 1 :(得分:0)
您在底部将ruch设置为false,因此每次单击一次布尔值始终为false,我认为它将是第二个玩家。也许这就是为什么您不能切换回第一个?
答案 2 :(得分:0)
class mouseListener implements MouseListener {
boolean roch = true;
public void mouseClicked(MouseEvent me) {
if (!roch){
System.out.println("I am player2");
roch = true;
} else {
System.out.println("I am player1");
roch = false;
}
}
public void mousePressed(MouseEvent me) {
}
public void mouseReleased(MouseEvent me) {
}
public void mouseEntered(MouseEvent me) {
}
public void mouseExited(MouseEvent me) {
}
}
不确定这是否有帮助,但这是我做的可以解决您的问题的方法,我有一个鼠标侦听器来跟踪我的鼠标单击,并且“ public void mouseClick会改变播放器