Flash 动作脚本:moneycounter 代码运行不正确
Flash actionscript: moneycounter code not behaving correctly
我正在尝试在 Adobe Flash (Animate) 动画中设置虚拟巴士售票机。
前提是您必须在插槽中输入准确的车费,因为司机不携带零钱。确切票价为 2 美元。该机器接受 5 美分、10 美分、25 美分、1 美元、5 美元、10 美元和 20 美元的面额。如果输入的金额超过 2 美元,则会弹出一条消息,显示 "Exact fare only please. Drivers cannot provide change." 因此在这种情况下,只有 5 美分、10 美分、25 美分和 1 美元的面额有效,但必须输入它们以恰好加起来为 2 美元.
下面的代码对于 25 美分和 1 美元面额的单独和组合都可以正常工作,但是当 5 美分和 10 美分单独使用或与 25 美分和 1 美元一起使用时,它会出现错误。例如,如果您尝试仅使用 10 美分来达到 2 美元,则总额将达到 1.90 美元,但在您输入最后 10 美分时不会支付车费。而是触发弹出 "Exact fare only please" 消息。如果您随后添加 5 分硬币,总额将达到 1.95 美元,但当您输入最后一个 5 分硬币时,仍不会支付车费。而是再次触发弹出 "Exact fare only please." 消息。
输入 $1 然后再输入 $1 效果很好。输入 8 个季度与输入 $1 和 4 个季度一样有效。无法弄清楚问题是什么。将不胜感激任何帮助。
谢谢!
代码如下:
var Cash = 00.00
var payola = Money.text = "02.00";
Money.text = "$" + Cash.toFixed(2);
popup.visible = false;
nickel_btn.addEventListener(MouseEvent.CLICK, add5cents);
function add5cents(event:MouseEvent):void
{
nickelcoin_mc.gotoAndPlay ("nickel_pay");
Cash+=00.05;
Money.text = "$" + Cash.toFixed(2);
if(Cash == payola){
setTimeout(delay, 500);
function delay(){
gotoAndStop(46);
}
}
if(Cash > payola){
nickelcoin_mc.gotoAndPlay ("nickel_nopay");
Cash-=00.05;
Money.text = "$" + Cash.toFixed(2);
popup.visible = true;
}
else {
popup.visible = false;
}
}
dime_btn.addEventListener(MouseEvent.CLICK, add10cents);
function add10cents(event:MouseEvent):void
{
dimecoin_mc.gotoAndPlay ("dime_pay");
Cash+=00.10;
Money.text = "$" + Cash.toFixed(2);
if(Cash == payola){
setTimeout(delay, 500);
function delay(){
gotoAndStop(46);
}
}
if(Cash > payola){
dimecoin_mc.gotoAndPlay ("dime_nopay");
Cash-=00.10;
Money.text = "$" + Cash.toFixed(2);
popup.visible = true;
}
else {
popup.visible = false;
}
}
quarter_btn.addEventListener(MouseEvent.CLICK, add25cents);
function add25cents(event:MouseEvent):void
{
quartercoin_mc.gotoAndPlay ("quarter_pay");
Cash+=00.25;
Money.text = "$" + Cash.toFixed(2);
if(Cash == payola){
setTimeout(delay, 500);
function delay(){
gotoAndStop(46);
}
}
if(Cash > payola){
quartercoin_mc.gotoAndPlay ("quarter_nopay");
Cash-=00.25;
Money.text = "$" + Cash.toFixed(2);
popup.visible = true;
}
else {
popup.visible = false;
}
}
one_btn.addEventListener(MouseEvent.CLICK, add);
function add(event:MouseEvent):void
{
onebill_mc.gotoAndPlay ("one_pay");
Cash+=01.00;
Money.text = "$" + Cash.toFixed(2);
if(Cash == payola){
setTimeout(delay, 500);
function delay(){
gotoAndStop(46);
}
}
if(Cash > payola){
onebill_mc.gotoAndPlay ("one_nopay");
Cash-=01.00;
Money.text = "$" + Cash.toFixed(2);
popup.visible = true;
}
else {
popup.visible = false;
}
}
five_btn.addEventListener(MouseEvent.CLICK, add);
function add(event:MouseEvent):void
{
fivebill_mc.gotoAndPlay ("five_pay");
Cash+=05.00;
Money.text = "$" + Cash.toFixed(2);
if(Cash == payola){
setTimeout(delay, 500);
function delay(){
gotoAndStop(46);
}
}
if(Cash > payola){
fivebill_mc.gotoAndPlay ("five_nopay");
Cash-=05.00;
Money.text = "$" + Cash.toFixed(2);
popup.visible = true;
}
else {
popup.visible = false;
}
}
ten_btn.addEventListener(MouseEvent.CLICK, add);
function add(event:MouseEvent):void
{
tenbill_mc.gotoAndPlay ("ten_pay");
Cash+=10.00;
Money.text = "$" + Cash.toFixed(2);
if(Cash == payola){
setTimeout(delay, 500);
function delay(){
gotoAndStop(46);
}
}
if(Cash > payola){
tenbill_mc.gotoAndPlay ("ten_nopay");
Cash-=10.00;
Money.text = "$" + Cash.toFixed(2);
popup.visible = true;
}
else {
popup.visible = false;
}
}
twenty_btn.addEventListener(MouseEvent.CLICK, add);
function add(event:MouseEvent):void
{
twentybill_mc.gotoAndPlay ("twenty_pay");
Cash+=20.00;
Money.text = "$" + Cash.toFixed(2);
if(Cash == payola){
setTimeout(delay, 500);
function delay(){
gotoAndStop(46);
}
}
if(Cash > payola){
twentybill_mc.gotoAndPlay ("twenty_nopay");
Cash-=20.00;
Money.text = "$" + Cash.toFixed(2);
popup.visible = true;
}
else {
popup.visible = false;
}
}
stop();
这可能是浮点数引起的问题。尝试将其放入您的代码中并检查控制台输出:
// Expect result to be 0.1
trace(0.3-0.2);
0.09999999999999998
// Adding lots of 5 cent coins. Expect result to be 0.5
trace(0.05 + 0.05 + 0.05 + 0.05 + 0.05 + 0.05 + 0.05 + 0.05 + 0.05 + 0.05);
0.49999999999999994
您的 ==
比较有时会失败,因为将浮点数相加会产生意外值。这个答案很好地解释了问题:
对于您的情况,尝试将 Cash
变量更改为 int
,并存储美分而不是美元:
var Cash:int = 0;
var payola:int = 200; // 2 dollars
// Add 5 cents
Cash += 5;
// Adding 1 dollar
Cash += 100;
// Printing out the amount in dollars and cents
Money.text = "$" + (Cash / 100).toFixed(2);
// Our exact check is safe now that we avoid floats.
if(Cash == payola) { ... }
我正在尝试在 Adobe Flash (Animate) 动画中设置虚拟巴士售票机。
前提是您必须在插槽中输入准确的车费,因为司机不携带零钱。确切票价为 2 美元。该机器接受 5 美分、10 美分、25 美分、1 美元、5 美元、10 美元和 20 美元的面额。如果输入的金额超过 2 美元,则会弹出一条消息,显示 "Exact fare only please. Drivers cannot provide change." 因此在这种情况下,只有 5 美分、10 美分、25 美分和 1 美元的面额有效,但必须输入它们以恰好加起来为 2 美元.
下面的代码对于 25 美分和 1 美元面额的单独和组合都可以正常工作,但是当 5 美分和 10 美分单独使用或与 25 美分和 1 美元一起使用时,它会出现错误。例如,如果您尝试仅使用 10 美分来达到 2 美元,则总额将达到 1.90 美元,但在您输入最后 10 美分时不会支付车费。而是触发弹出 "Exact fare only please" 消息。如果您随后添加 5 分硬币,总额将达到 1.95 美元,但当您输入最后一个 5 分硬币时,仍不会支付车费。而是再次触发弹出 "Exact fare only please." 消息。
输入 $1 然后再输入 $1 效果很好。输入 8 个季度与输入 $1 和 4 个季度一样有效。无法弄清楚问题是什么。将不胜感激任何帮助。 谢谢!
代码如下:
var Cash = 00.00
var payola = Money.text = "02.00";
Money.text = "$" + Cash.toFixed(2);
popup.visible = false;
nickel_btn.addEventListener(MouseEvent.CLICK, add5cents);
function add5cents(event:MouseEvent):void
{
nickelcoin_mc.gotoAndPlay ("nickel_pay");
Cash+=00.05;
Money.text = "$" + Cash.toFixed(2);
if(Cash == payola){
setTimeout(delay, 500);
function delay(){
gotoAndStop(46);
}
}
if(Cash > payola){
nickelcoin_mc.gotoAndPlay ("nickel_nopay");
Cash-=00.05;
Money.text = "$" + Cash.toFixed(2);
popup.visible = true;
}
else {
popup.visible = false;
}
}
dime_btn.addEventListener(MouseEvent.CLICK, add10cents);
function add10cents(event:MouseEvent):void
{
dimecoin_mc.gotoAndPlay ("dime_pay");
Cash+=00.10;
Money.text = "$" + Cash.toFixed(2);
if(Cash == payola){
setTimeout(delay, 500);
function delay(){
gotoAndStop(46);
}
}
if(Cash > payola){
dimecoin_mc.gotoAndPlay ("dime_nopay");
Cash-=00.10;
Money.text = "$" + Cash.toFixed(2);
popup.visible = true;
}
else {
popup.visible = false;
}
}
quarter_btn.addEventListener(MouseEvent.CLICK, add25cents);
function add25cents(event:MouseEvent):void
{
quartercoin_mc.gotoAndPlay ("quarter_pay");
Cash+=00.25;
Money.text = "$" + Cash.toFixed(2);
if(Cash == payola){
setTimeout(delay, 500);
function delay(){
gotoAndStop(46);
}
}
if(Cash > payola){
quartercoin_mc.gotoAndPlay ("quarter_nopay");
Cash-=00.25;
Money.text = "$" + Cash.toFixed(2);
popup.visible = true;
}
else {
popup.visible = false;
}
}
one_btn.addEventListener(MouseEvent.CLICK, add);
function add(event:MouseEvent):void
{
onebill_mc.gotoAndPlay ("one_pay");
Cash+=01.00;
Money.text = "$" + Cash.toFixed(2);
if(Cash == payola){
setTimeout(delay, 500);
function delay(){
gotoAndStop(46);
}
}
if(Cash > payola){
onebill_mc.gotoAndPlay ("one_nopay");
Cash-=01.00;
Money.text = "$" + Cash.toFixed(2);
popup.visible = true;
}
else {
popup.visible = false;
}
}
five_btn.addEventListener(MouseEvent.CLICK, add);
function add(event:MouseEvent):void
{
fivebill_mc.gotoAndPlay ("five_pay");
Cash+=05.00;
Money.text = "$" + Cash.toFixed(2);
if(Cash == payola){
setTimeout(delay, 500);
function delay(){
gotoAndStop(46);
}
}
if(Cash > payola){
fivebill_mc.gotoAndPlay ("five_nopay");
Cash-=05.00;
Money.text = "$" + Cash.toFixed(2);
popup.visible = true;
}
else {
popup.visible = false;
}
}
ten_btn.addEventListener(MouseEvent.CLICK, add);
function add(event:MouseEvent):void
{
tenbill_mc.gotoAndPlay ("ten_pay");
Cash+=10.00;
Money.text = "$" + Cash.toFixed(2);
if(Cash == payola){
setTimeout(delay, 500);
function delay(){
gotoAndStop(46);
}
}
if(Cash > payola){
tenbill_mc.gotoAndPlay ("ten_nopay");
Cash-=10.00;
Money.text = "$" + Cash.toFixed(2);
popup.visible = true;
}
else {
popup.visible = false;
}
}
twenty_btn.addEventListener(MouseEvent.CLICK, add);
function add(event:MouseEvent):void
{
twentybill_mc.gotoAndPlay ("twenty_pay");
Cash+=20.00;
Money.text = "$" + Cash.toFixed(2);
if(Cash == payola){
setTimeout(delay, 500);
function delay(){
gotoAndStop(46);
}
}
if(Cash > payola){
twentybill_mc.gotoAndPlay ("twenty_nopay");
Cash-=20.00;
Money.text = "$" + Cash.toFixed(2);
popup.visible = true;
}
else {
popup.visible = false;
}
}
stop();
这可能是浮点数引起的问题。尝试将其放入您的代码中并检查控制台输出:
// Expect result to be 0.1
trace(0.3-0.2);
0.09999999999999998
// Adding lots of 5 cent coins. Expect result to be 0.5
trace(0.05 + 0.05 + 0.05 + 0.05 + 0.05 + 0.05 + 0.05 + 0.05 + 0.05 + 0.05);
0.49999999999999994
您的 ==
比较有时会失败,因为将浮点数相加会产生意外值。这个答案很好地解释了问题:
对于您的情况,尝试将 Cash
变量更改为 int
,并存储美分而不是美元:
var Cash:int = 0;
var payola:int = 200; // 2 dollars
// Add 5 cents
Cash += 5;
// Adding 1 dollar
Cash += 100;
// Printing out the amount in dollars and cents
Money.text = "$" + (Cash / 100).toFixed(2);
// Our exact check is safe now that we avoid floats.
if(Cash == payola) { ... }