基本上我正在制作一个需要为角色添加健康的游戏。当玩家击中蜘蛛的命中框时,我希望改变生命值并重新绘制我的框架。我觉得我做得对。这是我的代码:
Frame.java
:
public static int redHealth;
public static int blueHealth;
public static void getBlueHealth(int health) {
health = redHealth;
}
public static void getRedHealth(int health) {
health = blueHealth;
}
// Create health labels
public static JLabel redHealthLabel = new JLabel("Health: " + redHealth);
public static JLabel blueHealthLabel = new JLabel("Health: " + blueHealth);
Key.java
:
if (Frame.spiderObj.intersects(Frame.blueCharacterObj)) {
System.out.println("hit spider");
Frame.getBlueHealth(97);
Frame.frame.repaint();
System.out.println(Frame.redHealth + "" + Frame.blueHealth);
}
如您所见,我将97
作为参数传递给getRedHealth()
andgetBlueHealth()
方法,然后将其设置为等于redHealth
and blueHealth
。但是,当我运行游戏并击中蜘蛛时,它会打印出来hit spider
但不会更新健康状况。
有什么帮助???