私は、GUIで常に異なる値を表示しようとしているSwingライブラリを使って、簡単な2次元プラットフォームゲームを作っています。値は更新され、最初のレベルでは完全に機能しますが、次のレベルでは同じままです。 GUI用シンプルゲームでGUIが値を更新しない
コード:
public class MyView extends UserView {
Player player;
private Image background;
private Image newBackground;
public MyView(World world, Player player, int width, int height) {
super(world, width, height);
this.player = player;
this.background = new ImageIcon("data/rainy.gif").getImage()
.getScaledInstance(1000, 1000, 1000);
}
@Override
protected void paintForeground(Graphics2D g) {
g.drawString("Lives left: " + player.getLifeCount(), 650, 20);
g.drawString("Blue hearts picked: " + player.getBlueHeartCount(), 650, 30);
g.drawString("Time left: " + player.getTimeCounter(), 650, 50);
}
}
第二レベル、player.getLifeCount()
、player.getBlueHeartCount()
とplayer.getTimeCounter()
に達し、凍結及び更新を停止します。ここに関連するコードである
public class Level2 extends GameLevel implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// System.out.println("Action event!");
if (getPlayer().heartPick) {
timer.stop();
} else if (player.getTimeCounter() <= 0) {
System.exit(0);
} else {
counter -= 1;
player.setTimeCounter(counter);
}
}
}
:
public class Level1 extends GameLevel implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (getPlayer().heartPick) {
timer.stop();
} else if (player.getTimeCounter() <= 0) {
System.exit(0);
} else {
counter -= 1;
player.setTimeCounter(counter);
}
}
コードレベル2の場合:ここ
は、値が変更されたレベル1とレベル2(輸入せずに)関連するコードでありますメインクラス:
public class Game {
private GameLevel world;
private MyView view;
private int level;
// timer
private Timer timer;
public Game(){
// make the world
level = 1;
world = new Level1();
world.populate(this);
// make a view
view = new MyView(world, world.getPlayer(), 800, 600);
// display the view in a frame
final JFrame frame = new JFrame("Event handling");
// start!
world.start();
}
// return world
public GameLevel getWorld() {
return world;
}
// the player in current level
public Player getPlayer() {
return world.getPlayer();
}
// Progress to next level
public void goNextLevel() {
world.stop();
switch (level) {
case 4:
System.exit(0);
case 2:
level++;
// get a new world
world = new Level2();
// fill world
world.populate(this);
// show the new world in the view
view.setWorld(world);
view.setBackground(2);
world.start();
break;
case 3:
level++;
//get new world
world = new Level3();
//fill world
world.populate(this);
// show the new world in the view
view.setWorld(world);
view.setBackground(3);
world.start();
break;
default:
level++;
break;
}
}
}
コードを簡素化し、問題自体に焦点を当ててください。このコードをすべて調べて、問題を見つけようとするのは難しいです。このような簡略化された例を準備することは、問題の発見にも役立ちます。 – ehh
ありがとうございます。私はコードを合理的なものとして保つためにいくつかの変更を加えました。これは私が初めてStackoverflowで質問を書いたものです。私に知らせてくれてありがとう。 – 4kiomo
あなたのコードは、私たちがアクセスできないライブラリを大量に使用しているため、テストするバグやバグの原因を理解することができません。これはあなた自身では完全に可能かもしれませんが、@ehhが示唆するように、問題の原因となっているコアを分離できるようになるまで、自分のコードを単純化します。 –