do-while ループで JButton を繰り返し (タイマーで使用して) 更新するのに問題があります。私は、100 個のボタンを持つ JButton arrayList に対応するタイル オブジェクトの 10 * 10 グリッドでプレイされる単純なゲームに取り組んでいます。
プログラムのこの部分は、単純なパスファインディングを処理します (つまり、キャラクターをクリックしてから空のタイルをクリックすると、キャラクターは目的地に向かう途中で各タイルを移動します)。ユーザーがキャラクターの進行状況を確認できるように、各ステップの間に遅延があります。
現在の状態では、動きは正しいですが、JButton は、中間ステップではなく、キャラクターが目的地に到達したときにのみ更新されます。
public void move(int terrainTile)
{
int currentPosition = actorList.get(selectedActor).getPosition();
int movementValue = 0;
int destination = terrainTile;
int destinationX = destination / 10;
int destinationY = destination % 10;
do
{
currentPosition = actorList.get(selectedActor).getPosition(); // Gets PC's current position (before move)
System.out.println("Old position is " + currentPosition);
int currentX = currentPosition / 10;
int currentY = currentPosition % 10;
if(actorList.get(selectedActor).getCurrentAP() > 0)
{
movementValue = 0;
if(destinationX > currentX)
{
movementValue += 10;
}
if(destinationX < currentX)
{
movementValue -= 10;
}
if(destinationY > currentY)
{
movementValue += 1;
}
if(destinationY < currentY)
{
movementValue -= 1;
}
int nextStep = currentPosition + movementValue;
myGame.setActorIdInTile(currentPosition, -1); //Changes ActorId in PC current tile back to -1
scrubTiles(currentPosition);
actorList.get(selectedActor).setPosition(nextStep); // Sets new position in actor object
System.out.println("Actor " + selectedActor + " " + actorList.get(selectedActor).getName() + " position has been updated to " + nextStep);
myGame.setActorIdInTile(nextStep, selectedActor); // Sets ActorId in moved to Tile
System.out.println("Tile " + nextStep + " actorId has been updated to " + selectedActor);
buttons.get(nextStep).setIcon(new ImageIcon(actorList.get(selectedActor).getImageName()));
// If orthagonal move AP-4
if(movementValue == 10 || movementValue == -10 || movementValue == 1 || movementValue == -1)
{
actorList.get(selectedActor).reduceAP(4);
}
// If diagonal move AP-6
else
{
actorList.get(selectedActor).reduceAP(6);
}
System.out.println(actorList.get(selectedActor).getName() + " has " + actorList.get(selectedActor).getCurrentAP() + " AP remaining");
try
{
Thread.sleep(500); // one second
}
catch (Exception e){}
buttons.get(nextStep).repaint();
}
else
{
System.out.println(actorList.get(selectedActor).getName() + " has insufficient AP to move");
break;
}
}while(destination != (currentPosition + movementValue));
私が試したこと:
buttons.get(nextStep).repaint(); (imageIconを設定した後、ボタンを再描画するコマンドを入れてみました。変化なし。
buttons.get(nextStep).revalidate(); (これが何をするのか 100% 確信はありません - 潜在的な解決策として思いつきましたが、うまくいきません。
ステップ 1 と 2 の組み合わせ
スイングタイマークラスを調べました-actionEventが発生するたびに動きが発生するわけではありません(キャラクターが選択され、ターゲットタイルが空の場合のみ)ので、これをどのように機能させることができるかわかりません
どんな助けでも大歓迎です!