Article

Perform Math Calculations in Flash

Page: 1 2 3 Next

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.

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.

The ActionScript in Flash would look like this:

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.

  1. Create a new movie.

  2. Create four textboxes in the first frame of the movie, and arrange them vertically.

  3. 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.

  4. 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.

    1124_image501

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

    1124_image502

  6. Next, add some static text to the left of the text boxes. Starting from the top to the bottom:
    • Number 1
    • Number 2
    • Sum of Number 1 and Number 2
    • Product of Number 1 and Number 2

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

    1124_image6

  7. 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:
    • number_one
    • number_two
    • result_sum
    • result_product


    1124_image7

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

  9. Drag and drop the PushButton component into the scene.

    1124_image8

  10. 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.

    1124_image9

  11. 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.

    1124_image10

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

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links