flash as3数学计算

tech2024-05-27  82

flash as3数学计算

The days of Flash being used solely for entertainment are over. Macromedia has spent an enormous amount of money and effort to bring Flash out of its “animation only” box, and promote it as a cross-platform development environment, as well as a designer environment. Along with the funny cartoons, Flash can now be used to build business applications.

Flash仅用于娱乐的时代已经过去。 Macromedia已花费大量金钱和精力将Flash从其“仅动画”盒子中移除,并将其推广为跨平台开发环境和设计器环境。 随同有趣的卡通漫画,Flash现在可以用于构建业务应用程序。

One of the basic requirements of any business app is the ability to do math. In this tutorial, we will cover how to do basic arithmetic in Flash. We’ll also cover the capture of data input by a user, and learn how to perform calculations based on this data.

任何业务应用程序的基本要求之一是数学能力。 在本教程中,我们将介绍如何在Flash中进行基本算术。 我们还将介绍如何捕获用户输入的数据,并学习如何根据此数据执行计算。

搜索有关Flash计算能力的信息 (The Search for Information on Flash’s Calculation Abilities)

A little over a year ago, I started a project that required Flash to do some serious calculations. I knew that calculations were possible in Flash; it was just that I’d never done any. So, like any good Web user, I started to search forums and Google for some guidance. Unfortunately, I found very little information on what I was looking for.

一年多以前,我启动了一个项目,该项目要求Flash进行一些认真的计算。 我知道可以在Flash中进行计算; 只是我什么都没做。 因此,像任何优秀的Web用户一样,我开始在论坛和Google上搜索以获取一些指导。 不幸的是,我发现的信息很少。

I then went to my local bookstore in search of answers, but found the same problem: there is virtually no documentation available on using math in real world Flash applications.

然后,我去了当地的书店寻找答案,但是发现了同样的问题:在现实世界的Flash应用程序中,几乎没有关于使用数学的文档。

Don’t believe me? Search on Macromedia.com for the term “calculation”. Go to your local bookstore, look at any book claiming to be the be-all and end-all and for Flash, and see what you can find under “math” or “equations”. Sure, there are plenty of examples the explain the use of math to make random swirling lines, flying squares, and dancing midgets, but what about using math to calculate data? Those dancing midgets are cute, but calculating data can make money.

不相信我吗 在Macromedia.com上搜索“计算”一词。 前往您当地的书店,查看任何声称是Flash的万物之宝,并查看可以在“数学”或“等式”下找到的内容。 当然,有很多示例说明了如何使用数学来制作随机的漩涡线,飞行方块和跳舞的侏儒,但是使用数学来计算数据又如何呢? 那些跳舞的侏儒很可爱,但是计算数据可以赚钱。

After looking through about 30 books, I found two that offered about a page on what I was looking for. It wasn’t much, but it was something.

在浏览了大约30本书之后,我发现其中两本书提供了有关所寻找内容的页面。 虽然不多,但确实如此。

If Rich Internet Applications are going to be as big as Macromedia hopes, more tutorials and examples covering basic, real-world business needs like calculating data will need to be written. This tutorial is the first attempt to pique developer awareness of, and interest in, Flash’s math capabilities.

如果Rich Internet Applications的规模将达到Macromedia的期望,那么就需要编写更多的教程和示例来满足基本的,实际的业务需求,例如计算数据。 本教程是激发开发人员对Flash数学功能的认识和兴趣的首次尝试。

Flash can handle any equation you throw at it, from simple addition, to complex calculus.

从简单的加法到复杂的演算,Flash都能处理您遇到的所有方程式。

静态计算 (Static Calculations)

To get you acquainted with what Flash can do, we’ll start off with simple, static calculations. By “static”, I mean equations that are hard coded, and do not require any input from the end user.

为了使您熟悉Flash的功能,我们将从简单的静态计算开始。 “静态”是指经过硬编码的等式,不需要最终用户输入。

All these calculations are performed with ActionScript, so we’ll be doing a little coding. Let’s go!

所有这些计算都是使用ActionScript执行的,因此我们将做一些编码。 我们走吧!

Start up Flash, and create a new movie.

启动Flash,然后创建一个新电影。

Draw four dynamic textboxes on your stage. The dynamic property is set on the Properties panel.

在舞台上绘制四个动态文本框。 动态属性在“属性”面板上设置。

Once that’s done, give each textbox a Variable name. In this example, I’m using “addition”, “subtraction”, “multiplication”, and “division” as the Variable names.

完成后,为每个文本框指定一个变量名称。 在此示例中,我使用“加法”,“减法”,“乘法”和“除法”作为变量名称。

Tip: When developing, you should always give objects (textboxes, components, Movieclips, etc.) unique names. Otherwise, you run the risk of confusing Flash. This would be like having two brothers named John and John, and having you mom say, "Tell John to wash the car and tell John to clean the bathroom.” Avoid the confusion by giving every object a unique name.

提示:开发时,应始终给对象(文本框,组件,Movieclips等)唯一的名称。 否则,您将有使Flash混淆的风险。 这就像有两个兄弟分别叫约翰和约翰,而让妈妈说:“告诉约翰洗车,告诉约翰打扫浴室。” 通过为每个对象赋予唯一的名称来避免混淆。

We’ll be using these variables to tell Flash were to display the results of our calculations.

我们将使用这些变量告诉Flash显示我们的计算结果。

In the timeline, create a new layer. Name the layer containing the textboxes “Calculations”, and name the new layer “Actions”. We do this so that we can easily see which layer contains the code, and which layer contains the user interface.

在时间轴中,创建一个新层。 将包含文本框的层命名为“计算”,并将新层命名为“动作”。 我们这样做是为了我们可以轻松地看到哪一层包含代码,哪一层包含用户界面。

Now, let’s apply the calculations. Select the first frame in your Actions layer. In order to give ourselves freedom to type what we wish, we need to set the Actions panel to Expert Mode. Choose Expert Mode from the Actions panel pop-up menu (at the upper right of the panel).

现在,让我们应用计算。 在“动作”层中选择第一帧。 为了使自己能够自由键入所需内容,我们需要将“动作”面板设置为“专家模式”。 从“动作”面板弹出菜单(在面板右上方)中选择“专家模式”。

Now, input the following code:

现在,输入以下代码:

addition = 1+1; subtraction = 5-2; multiplication = 10*2; division = 100/5;

Now, an explanation of what this code does and why. There are four lines, and at the beginning of each is a Variable reference. Remember the Variable names we gave to the four textboxes? The first line starts with “addition”. This references our “addition” textbox. We then give an expression to which "addition” is equal. The “addition” textbox will display the results of one plus one, the subtraction textbox will display the results of five minus two, etc.

现在,说明此代码的作用以及原因。 有四行,每行的开头是一个变量引用。 还记得我们给四个文本框指定的变量名称吗? 第一行以“加法”开头。 这引用了我们的“附加”文本框。 然后,我们给出一个等于“加法”的表达式,“加法”文本框将显示1加1的结果,减法文本框将显示5减2的结果,依此类推。

Publish your movie to see the results!

发布您的电影以查看结果!

If you get really lost, here’s a sample FLA that has all the details.

如果您真的迷路了, 这里有一个包含所有详细信息的示例FLA 。

高等数学 (Advanced Math)

Now that you understand how to perform basic calculations, mixing them up is just a matter of algebra. The same math rules apply.

现在您已经了解了如何执行基本计算,将它们混合起来只是代数问题。 相同的数学规则适用。

sample = 10*2-6/3;

The above sample would give you a result of 18.

上面的示例将为您提供18的结果。

As in algebra, multiplications and divisions are calculated before additions and subtractions, and the use of parentheses specifies that anything in the parentheses will be calculated first. Using parentheses can deliver different results. For example, 10*(3-2) will give you a result of 10, where 10*3-2 will give you a result of 28.

像在代数中一样,乘法和除法是在加法和减法之前计算的,括号的使用指定将首先计算括号中的任何内容。 使用括号可以产生不同的结果。 例如,10 *(3-2)的结果为10,10 * 3-2的结果为28。

The ActionScript in Flash would look like this:

Flash中的ActionScript如下所示:

sample = 10(3-2);

Calculating User-Entered Data

计算用户输入的数据

Calculating static data is helpful, but calculating data that’s input by a user is powerful and sellable. The possibilities are endless, but here, we’ll cover basic data entry and calculation. Our sample will take two numbers entered by the user. On the click of a button, the Flash movie will display the sum, and the product of the numbers.

计算静态数据很有帮助,但是计算用户输入的数据功能强大且可出售。 可能性是无限的,但是在这里,我们将介绍基本数据输入和计算。 我们的示例将使用用户输入的两个数字。 单击按钮,Flash影片将显示总和以及数字乘积。

Create a new movie.

制作新电影。 Create four textboxes in the first frame of the movie, and arrange them vertically.

在影片的第一帧中创建四个文本框,并垂直排列它们。 In the Properties panel, set the textboxes to be left-aligned. The top two textboxes should be Input text, while the bottom two should be Dynamic text.

在“属性”面板中,将文本框设置为左对齐。 顶部的两个文本框应该是输入文本,而底部的两个文本框应该是动态文本。

Limit the textboxes to numbers only. To do this, just select a textbox and click the Character… button in the Properties panel. This will bring up the Character Options box. From there, select “Only”, “Numerals (0-9)”, and click Done.

将文本框限制为仅数字。 为此,只需选择一个文本框,然后单击“属性”面板中的“字符...”按钮。 这将弹出“字符选项”框。 在此处,选择“仅”,“数字(0-9)”,然后单击“完成”。

I’ve added a line in the middle of my textboxes, to give them some sex appeal!

我在文本框的中间添加了一行,以使它们更具吸引力!

Next, add some static text to the left of the text boxes. Starting from the top to the bottom:

接下来,在文本框的左侧添加一些静态文本。 从上到下:

Number 1

1号 Number 2

2号 Sum of Number 1 and Number 2

1号和2号之和 Product of Number 1 and Number 2

1号和2号的乘积

Next, add some static text to the left of the text boxes. Starting from the top to the bottom:

接下来,在文本框的左侧添加一些静态文本。 从上到下:

This is done to clearly label everything for the end user — us!

这样做是为了为最终用户-我们清楚地标记所有内容!

The textboxes need to be given Variable names, so that Flash knows what we’re talking about when we refer to them. The Variable for a textbox is defined in the Properties panel. Starting from the top to the bottom, name the textboxes accordingly:

需要为文本框指定变量名称,以便Flash知道在引用它们时我们在说什么。 文本框的变量在“属性”面板中定义。 从顶部到底部,分别为文本框命名:

number_one

第一 number_two

第二 result_sum

result_sum result_product

result_product

The textboxes need to be given Variable names, so that Flash knows what we’re talking about when we refer to them. The Variable for a textbox is defined in the Properties panel. Starting from the top to the bottom, name the textboxes accordingly:

需要为文本框指定变量名称,以便Flash知道在引用它们时我们在说什么。 文本框的变量在“属性”面板中定义。 从顶部到底部,分别为文本框命名:

Now, we need to add our button used to execute our calculations. In the Component panel, select Flash UI Components.

现在,我们需要添加用于执行计算的按钮。 在“组件”面板中,选择“ Flash UI组件”。

Drag and drop the PushButton component into the scene.

将PushButton组件拖放到场景中。

Select the PushButton component you’ve just dragged into your scene. In the Properties panel, label the component “Calculate” and set the Click Handler to “onCalculate”. This is the button we will use to calculate the user’s data.

选择您刚刚拖入场景的PushButton组件。 在“属性”面板中,将组件标记为“计算”,然后将“单击处理程序”设置为“ onCalculate”。 这是我们用来计算用户数据的按钮。

As a final measure, I have given the “Number 1” and “Number 2” textboxes a value of zero. When you’re done, you’re scene should look something like the image below.

最后,我给“数字1”和“数字2”文本框指定了零值。 完成后,您的场景应类似于下图。

Ok. We have our scene set up, so now, it’s time to put in the code that will make this movie work.

好。 我们已经设置好场景,现在是时候添加使这部电影正常工作的代码了。

The Code

代码

Name the current layer in the movie “content”. Add a new layer to the movie, and call it “actions”.

将电影中的当前图层命名为“内容”。 在影片中添加一个新层,并将其称为“动作”。

Select the first frame of the “actions” layer. In the Actions panel, add the following code:

选择“动作”层的第一帧。 在“动作”面板中,添加以下代码:

function onCalculate() {    one = Number(number_one);    two = Number(number_two);    result_sum = one + two;    result_product = one * two;   }

This is the code that will make this movie do its magic! Let’s look at what this code does and why.

这是使这部电影发挥作用的代码! 让我们看看这段代码的作用以及原因。

function onCalculate() {

This first line starts a new function in the movie. Remember that we gave our PushButton component a Click Handler of “onCalculate”. When this button is clicked, it will execute the code within this function.

第一行启动影片中的新功能。 请记住,我们为PushButton组件提供了“ onCalculate”的Click Handler。 单击此按钮后,它将执行此功能中的代码。

one = Number(number_one);   two = Number(number_two);

This code has two purposes. The first is to give shorter variable names to the data that’s being calculated. Instead of spelling out “number_one” throughout our code, we can now just use “one”.

此代码有两个目的。 第一种是为正在计算的数据提供较短的变量名。 现在我们无需在整个代码中拼写“ number_one”,而只需使用“ one”即可。

This makes more sense when taken in conjunction with the second purpose: to treat these variables as numbers. This is done with Number(), which tells Flash that we to treat values in parentheses as numbers. If we don’t, when we calculate 1 plus 1, we’ll get 11. Or, if we calculate 1 plus 2, we’ll get 12. Instead, with the Number(), when we calculate 1 plus 1, we’ll get 2.

与第二个目的结合使用时,这更有意义:将这些变量视为数字。 这是通过Number()完成的,它告诉Flash我们将括号中的值视为数字。 如果不这样做,当我们计算1加1时,我们将得到11。或者,如果我们计算1加2,我们将得到12。相反,使用Number(),当我们计算1加1时,我们会得到2。

result_sum = one + two;   result_product = one * two;   }

Remember the variable names we gave the two textboxes at the bottom? This code puts our calculation results within those textboxes. When this code is executed, the “result_sum” textbox will display the result of adding Number 1 and Number 2. The “result_product” textbox will display the result of multiplying them.

还记得我们在底部的两个文本框中输入的变量名吗? 此代码将我们的计算结果放在这些文本框中。 执行此代码后,“ result_sum”文本框将显示将数字1和数字2相加的结果。“ result_product”文本框将显示将它们相乘的结果。

That’s it! Publish your movie, type in some numbers in first two textboxes, and hit Calculate.

而已! 发布影片,在前两个文本框中键入一些数字,然后单击“计算”。

Again, if you get lost, download the sample FLA.

同样,如果您迷路了,请下载示例FLA 。

最后的想法 (Final Thoughts)

There is your quick and dirty run down of basic math in Flash. There is not a single calculation that Flash cannot handle. This tutorial only scratches the surface, but I hope it’s got you thinking about Flash math. For more information, try:

在Flash中,您的基本数学运算既快速又肮脏。 没有Flash无法处理的单个计算。 本教程仅涉及表面知识,但我希望它能使您考虑Flash数学。 有关更多信息,请尝试:

Flash Kit’s Math & Physics Forum

Flash Kit的数学与物理论坛

Basic Algebra Lessons

基础代数课程

翻译自: https://www.sitepoint.com/math-calculations-flash/

flash as3数学计算

最新回复(0)