我正在尝试检查玩家是否有足够的钱购买路径。我正在转换为返回currentMoney
变量的方法。无论出于何种原因,当货币类中的int currentMoney
更新时,它不会在路径类中更新。我是初学者,非常感谢您的解释!
这是我的路径代码:
public void clickedOn()
{
MyWorld world = (MyWorld) getWorld();
if (Greenfoot.mouseClicked(this)) {
if (money.getCurrentMoney() /* <--not updating */ > 2600)
{
moneydollars = true;
if (isClicked)
{
world.deductMoney(20);
world.addMaking(5);
canAfford = true;
isClicked = false;
}
else
{
isClicked = true;
}
}
else
{
moneydollars = false;
JOptionPane.showMessageDialog(null,"Oh No! You can't afford that.");
}
}
}
这是我的钱类代码:
public class Money extends Actor
{
private int stringLength;
private String text;
public int currentMoney;
private int moneyMaking;
private int fps = 0;
// DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG
public boolean debug = false;
// DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG
String abc;
MyWorld world = (MyWorld) getWorld();
/**
* Constructor: checks debug, updates image.
*
*/
public Money()
{
if(debug == true) {
add(99999);
}
else
{
add(2601);
}
updateImage();
}
public void act()
{
updateImage();
fps = fps + 1;
moneyMake();
}
public int getCurrentMoney()
{
return currentMoney;
}
public int getMoneyMaking()
{
return moneyMaking;
}
public void updateImage()
{
setImage(new GreenfootImage( "Money: $" + currentMoney, 30, Color.BLACK, Color.ORANGE));
}
public void add(int x)
{
currentMoney += x;
}
public void moneyMake()
{
if (fps == 68)
{
currentMoney = currentMoney + moneyMaking;
fps = 0;
}
}
public void addMaking(int x)
{
moneyMaking += x;
}
}
如果这还不够,请评论并询问您的需求。我可能犯了一个简单的错误,但我看不到它。