如何在有条件的情况下增加或减少AS3中的显示数字变量? [英] How to raise or decrease a display number variable in AS3 with a condition?

查看:88
本文介绍了如何在有条件的情况下增加或减少AS3中的显示数字变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Toolbar.as类中,我在动态文本框中显示了一个简单的变量(4位数字),如下所示:

I've got, in my Toolbar.as class, a simple variable (4-digit number) displayed in a dynamic text box, like that :

var number = 9999; 
useText.text = String(number); 
trace(number);

在我的拼图班中,我有一个条件,如果是真的,我想减少这个数字. 我怎样才能做到这一点 ?

In my puzzle class I have a condition and I would like to decrease the number if it's true. How can I do that ?

例如,我在我的Puzzle.as类中:

For exemple, I've got, in my Puzzle.as class :

if (inv.containsItem("rock")) {
    toolbar.useText.text = "String(number)" - 100;
    }

但是它不起作用(我知道我必须更改"String(number)"-100,但是我应该放什么?

But it doesn't work (I know that I have to change the "String(number)" - 100 but what should I put ?

谢谢!

推荐答案

我相信您正在尝试这样做:String(number - 100);

I believe you are attempting to do: String(number - 100);

这称为包装器或强制转换,您正在使用-100的操作将数字变量强制转换为String.

The is called a wrapper or casting, you are casting the number variable with the operation of - 100 to a String.

如果您想获取字段中的当前文本并使用它:

If you want to grab the current text in your field and use that:

useText.text = String( Number( useText.text ) - 100 );

这会将当前文本转换为Number,减去100,然后将其转换回String

This casts the current text to a Number, subtracts 100, then casts it back to a String

这篇关于如何在有条件的情况下增加或减少AS3中的显示数字变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆