• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Hour 15. ActionScripting Applications fo... > Task: Increment a Variable

Task: Increment a Variable

This task demonstrates incrementing a variable and then revealing it to the user. Here are the steps:

In a new file, create a button and a Dynamic Text block (and be sure to type a zero in it). Associate the variable count with the text. This automatically creates the count variable.

On the button, insert the evaluate Action (under Actions, Miscellaneous Actions). This Action lets you type anything you want. Type count++. This means to increment the value of the variable count. (That is, add 1 to it.) (See Figure 15.9.)

Figure 15.9. The Action attached to the button will increment the value of count.

Test the movie and you should see the count variable increment. Too easy, eh? Okay, change the variable associated with the text field to message. Test the movie again. Is it working? Just because you don't see the value of the count variable doesn't mean it's not changing every time the button is clicked. (Actually, as you'll see in Hour 23, “Learning How to Learn Flash,” the Debugger can be opened to reveal the value of all your variables.) Leave the “message” variable associated with the Dynamic Text.

Drag another instance of your button onto the Stage and insert another evaluate Action. Then type message="Your total is "+count.

When you test the movie, click the first button seven times. Then when you click the new button, the message text changes onscreen. The point is that your variable can be changing even though you don't see it onstage.

Finally, the only bug in this program comes if the user starts clicking the incrementing button after he or she has clicked the new totaling button. It creates the message fine the first time, but then the information becomes old. You should add an evaluate Action to the incrementing button (the first button). Within the same mouse event that's using count++ (maybe right below it), insert the evaluate Action and type message="". This way, the field will be cleared every time the user presses the increment button.



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint