Two ways to write ActionScript in your Flex application

Tuesday, December 14, 2010

Hello! This article is talking about how to write ActionScript in your flex application. There are two ways to write ActionScript in a Flex application. First option is putting ActionScript code within MXML file. This is quite common when handling events. But this approach is best for short pieces of ActionScript. You can place it within Script tags as below:

<fx:Script>
<! [CDATA[
// ActionScript goes here!
]]>
</fx:Script>


Note that Script is defined within the Flex Core components namespace, fx. This namespace isn’t used in the first two chapters, although Flash Builder adds fx:Declarations tags to the default MXML file.

Also, you should surround your code with the <![CDATA[ and ]]> tags Because the MXML file is XML, many characters commonly found within ActionScript will be invalid when the XML interpreter looks at the page. By wrapping the ActionScript within those tags, the ActionScript code will be ignored by the XML interpreter.

A second way to add ActionScript to a Flex application is to store it in a separate text file. There are two primary benefits to going this route and I really recommend this way. First, it makes it easier to navigate and develop your application, with content in multiple files, saving you from searching through increasingly large and complex MXML documents for any particular code.

The second benefit is that once you’ve separated some ActionScript into another file, you can easily reuse it in other projects. Although it’s considered to be a best practice to write your ActionScript in a separate file, most of the examples in many books you read will not do so, because for the sake of simplicity.A separate ActionScript file can then be included by, or imported into, the MXML document. From a functional standpoint, including an ActionScript file has the same effect as if the code were inserted directly into the MXML. So, up to you which way you want to choose to develop your flex projects. I hope you will enjoy coding and have more fun.

0 comments:

Post a Comment

 
 
 

Recent Posts

Recent Comments