Home arrow Flash arrow Page 2 - ActionScript in Flex Applications
FLASH

ActionScript in Flex Applications


If you want to create a fairly sophisticated program in Flex, you will need to use ActionScript. If you've never used the two languages together, this five-part series gets you started. It is excerpted from chapter four of the book Programming Flex 2, written by Chafic Kazoun and Joey Lott (O'Reilly, 2007; ISBN: 059652689X). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 4 stars4 stars4 stars4 stars4 stars / 8
May 01, 2008
TABLE OF CONTENTS:
  1. · ActionScript in Flex Applications
  2. · Using ActionScript
  3. · Nested ActionScript
  4. · MXML and ActionScript Correlations

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
ActionScript in Flex Applications - Using ActionScript
(Page 2 of 4 )

When you want to use ActionScript within Flex, you have four basic options for where to place the code:

  1. Inline within MXML tags
  2. Nested within MXML tags
  3. In MXML scripts
  4. Within ActionScript classes

The preceding lists the techniques for working with ActionScript code, from the simplest to the most complex form. We’ll look at each of these techniques in the following sections.

Inline ActionScript

Inline ActionScript appears within MXML tags. Believe it or not, you’ve already seen several examples of this in Chapter 3. Inline event handling and data binding using curly brace syntax necessarily uses basic ActionScript. The following example uses ActionScript to display an alert dialog box when the user clicks on a button:

  <mx:Button id="alertButton" label="Show Alert"
             click="mx.controls.Alert.show('Example')" />

In this example, the text assigned to theclickevent handler attribute is ActionScript code, which calls ashow()method of an ActionScript class calledAlert.

The next example uses data binding:

  <mx:VBox>
   
<mx:TextInput id="input" />
   
<mx:Text id="output"
text="{input.text}" />
  </mx:VBox>

This example uses the ActionScript expressioninput.textto evaluate thetextproperty value for theinputobject (the text input control).

Inline data binding represents the most limited use of ActionScript, because it can evaluate only one expression. For instance, the preceding example evaluates the expressioninput.text. You could use a more complex expression, such as the following:

  <mx:VBox>
   
<mx:TextInput id="input" />
   
<mx:Text id="output" text="{'User input: ' + input.text}" />
 
</mx:VBox>

This example concatenates the stringUser input:with the user input from the text input control. You can also create even more complex expressions using inline data binding.

Inline event handlers allow you to write more complex ActionScript that can consist of several statements. ActionScript statements generally end with semicolons. The following example illustrates a button with slightly more complex event handler code, consisting of two expressions:

  <mx:Button id="alertButton" label="Show Alert" click="mx.controls.Alert.
  show('Example');alertButton.x += 40;" />

This example first displays an alert dialog box. It then moves the button to the right by 40 pixels. Although you can string together many statements (as in this example), it is very uncommon. It’s not difficult to understand why this would be. Rather simply: the code is difficult to read and manage when you try to use several inline statements in that fashion. If an event handler needs to run several statements, it is far more common to simply call a function. We’ll look more at functions in the next section, and then later in the chapter, in the “Methods ” section.


blog comments powered by Disqus
FLASH ARTICLES

- More Top Flash Game Tutorials
- Top Flash Game Tutorials
- Best Flash Photo Gallery Tutorials
- The Top Flash Tutorials for Menus
- 7 Great Flash Tutorials
- Adobe Creative Suite 5.5 Now Available
- Critical Flash Vulnerability Heats Up the Web
- More on Nonpersistent Client-Side Remote Sha...
- Nonpersistent Client-Side Remote Shared Obje...
- Using the Decorator Pattern for a Real Web S...
- Using Concrete Decorator Classes
- Delving More Deeply into the Decorator Patte...
- The Decorator Pattern in Action
- A Simple Decorator Pattern Example
- Decorator Pattern

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials