我正在尝试创建收银机模拟游戏,并且我遇到了一个问题,我正在使用条件语句来检查是否正在向客户提供正确的更改以及是否按下按钮。在该游戏中,用户可以看到销售量以及客户支付的金额。有了这些信息,他们必须点击现金抽屉插槽,这将通过他们选择的货币单位的价值增加一个名为changeCount的变量。
如果按下changeCount = payment-sale和确认按钮,我正在使用if语句将它们带到下一帧。要检查按钮,我使用布尔值来查看它是否被按下。出于某种原因,它没有为if语句注册changeCount的更新值。有谁知道为什么会这样?如果我跟踪跟踪值(changeCount);它会返回我想要的值。 源代码粘贴在下面:
package
{
public class CashierMain extends MovieClip
{
private var confirmBtn:ConfirmButton;
private var pennyBtn:PennyButton;
private var dimeBtn:DimeButton;
private var nickelBtn:NickelButton;
private var quarterBtn:QuarterButton;
private var oneBtn:OneButton;
private var fiveBtn:FiveButton;
private var tenBtn:TenButton;
private var twentyBtn:TwentyButton;
private var changeCount:Number;
private var payment:int;
private var sale:Number;
private var clicked:Boolean;
private var total:Number;
public function startCashierMain()
{
//instantiating change count
changeCount = 0;
clicked = false;
sale = Math.random() * 50;
payment = Math.random() * 20 + sale;
total = payment - sale;
showPayment();
showChangeCount();
showSale();
showTotal();
//creating visuals
confirmBtn = new ConfirmButton();
confirmBtn.x = 450;
confirmBtn.y = 85;
addChild(confirmBtn);
pennyBtn = new PennyButton();
pennyBtn.x = 100;
pennyBtn.y = 50;
addChild(pennyBtn);
nickelBtn = new NickelButton();
nickelBtn.x = 175;
nickelBtn.y = 50;
addChild(nickelBtn);
dimeBtn = new DimeButton();
dimeBtn.x = 250;
dimeBtn.y = 50;
addChild(dimeBtn);
quarterBtn = new QuarterButton();
quarterBtn.x = 315;
quarterBtn.y = 50;
addChild(quarterBtn);
oneBtn = new OneButton();
oneBtn.x = 75;
oneBtn.y = 200;
addChild(oneBtn);
fiveBtn = new FiveButton();
fiveBtn.x = 200;
fiveBtn.y = 200;
addChild(fiveBtn);
tenBtn = new TenButton();
tenBtn.x = 325;
tenBtn.y = 200;
addChild(tenBtn);
twentyBtn = new TwentyButton();
twentyBtn.x = 450;
twentyBtn.y = 200;
addChild(twentyBtn);
//Listeners
pennyBtn.addEventListener(MouseEvent.CLICK,clickPenny);
nickelBtn.addEventListener(MouseEvent.CLICK,clickNickel);
dimeBtn.addEventListener(MouseEvent.CLICK,clickDime);
quarterBtn.addEventListener(MouseEvent.CLICK,clickQuarter);
oneBtn.addEventListener(MouseEvent.CLICK,clickOne);
fiveBtn.addEventListener(MouseEvent.CLICK,clickFive);
tenBtn.addEventListener(MouseEvent.CLICK,clickTen);
twentyBtn.addEventListener(MouseEvent.CLICK,clickTwenty);
confirmBtn.addEventListener(MouseEvent.CLICK, clickConfirm);
}
public function clickPenny(event:MouseEvent)
{
changeCount += .01;
showChangeCount();
}
public function clickNickel(event:MouseEvent)
{
changeCount += .05;
showChangeCount();
}
public function clickDime(event:MouseEvent)
{
changeCount += .10;
showChangeCount();
}
public function clickQuarter(event:MouseEvent)
{
changeCount += .25;
showChangeCount();
}
public function clickOne(event:MouseEvent)
{
changeCount += 1;
showChangeCount();
}
public function clickFive(event:MouseEvent)
{
changeCount += 5;
showChangeCount();
}
public function clickTen(event:MouseEvent)
{
changeCount += 10;
showChangeCount();
}
public function clickTwenty(event:MouseEvent)
{
changeCount += 20;
showChangeCount();
}
public function showChangeCount()
{
showChange.text = String("Change Tendered $ " + changeCount.toFixed(2));
}
public function showPayment()
{
paid.text = String("Amount Paid $ " + payment);
}
public function showSale()
{
totalSale.text = String("Cost of Sale " + sale.toFixed(2));
}
public function showTotal()
{
totaled.text = String("total $ " + total.toFixed(2));
}
public function clickConfirm(event:MouseEvent)
{
if (total == changeCount && clicked == false)
{
gotoAndPlay("next");
}
}
}
}
我是这个网站的新手,所以如果我的问题看起来很模糊,或者你不明白我的要求,请随时让我澄清一切。谢谢大家的帮助。
答案 0 :(得分:0)
首先,我不确定为什么你需要clicked
变量。您有事件监听器来查看是否单击了该按钮。
您得到了不匹配,因为total
和changeCount
实际上是不同的。由于Math.random()
生成的值介于0和1之间,因此您的sale
和payment
变量的变量在分数后面会有很多数字(如20.23515542354624等)。由于你没有对数字进行舍入,只在GUI中将它们显示为四舍五入(toFraction(2)
不对数字进行舍入,它只返回一个圆形表示的字符串)你最终比较20.23515542354624到20.23这样的数字,这显然是不平等的。
跟踪所有数字变量,如果你现在没有得到它,你会明白我的意思。
解决方案是将total
变量舍入为.xx格式,如下所示:
total = Math.round((payment - sale) * 100) / 100;
对不起,我的数学知识技术英语很糟糕,我使用了大量的术语。