Subscribe to our newsletter:
In his second article from the Flash to Flex series, which focuses on helping you migrate your work environment from Flash to Flex by learning the differences between and comparing the two applications, Kris Hadlock shows you all you need to know to get started using ActionScript 3. In this article you'll learn how to create packages and classes and incorporate them into a Flex project.
For years there has seemed to be a stigma in the development community about developing with AS (ActionScript). With the release of AS 2 there was a huge shift and many more developers made a transition to Flash; and with the ability to create classes, existing Flash developers were finally able to begin writing object-oriented code. AS has really matured over the years, and version 3 has raised the bar for object-oriented approaches by providing the ability to create packages, forcing strict typing through the compiler, and much more. In addition, the Flex environment has made it much easier to debug code and see errors or warnings while writing code.
The Flash to Flex series will focus on helping you migrate your work environment from Flash to Flex by learning the differences between and comparing the two applications. Last time, we covered how to integrate AS with MXML; this week we'll take it a step further by focusing on AS 3 and how to use it to create packages and classes and then incorporate them into a Flex project. If you have questions about creating a new Flex project, incorporating ActionScript, or triggering events, take a look at my previous article, "Integrating ActionScript with MXML."
Creating a Package
The concept of a package did not exist in previous versions of AS. In fact, classes were not even possible until the release of AS 2, and in AS 1 developers were left creating prototype objects, much like in JavaScript. Thankfully, AS 3 has kept the same concept of classes from AS 2 and has also incorporated packages.Packages are essentially the folder structure that you define for particular classes. For instance, my company is studiosedition.com, so the base package for all of my company classes will be the same as Listing 1.
Listing 1 Creating a base package
com.studioseditionListing 2 Creating a full class path in AS 2
com.studiosedition.utilities.AlertListing 3 Packages in ActionScript 3
package com.studiosedition.utilities
{
public class Alert
{
}
}
Figure 1: Creating a new ActionScript class with Flex
Note that the folder structure needs to exist in the Flex project in order to use it. For example, to use the package com.studiosedition.utilities, I needed to first create a com directory with a subdirectory named studiosedition, with another subdirectory named utilities.
Now that our class has been started we can take a look at how to start using it in the project.
Using a Custom Class
In the last article, "Integrating ActionScript with MXML," I showed how to create a simple method called recordEvent, which used the Alert class for notification of a few mouse events. In this article I will take that same method, add it to the new custom Alert class that we created, and show you how to use it in a project.First, take a look at Listing 4 to see how to add the recordEvent method to our class.
Listing 4 Creating a custom method in the Alert class
package com.studiosedition.utilities
{
public class Alert
{
public static function RecordEvent(event:String):void
{
mx.controls.Alert.show(event);
}
}
}The next thing to notice is the static keyword, which I added to allow the method to be called without class instantiation. This means that you can simply call Alert.RecordEvent()instead of creating a new Alert object before calling the method.
With our new method in place, let's start calling it from the MXML file that is created when a project is started. Listing 5 shows how to import our new Alert class and use a button to trigger the RecordEvent method.
Listing 5 Incorporating AS 3 with MXML
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script><![CDATA[import com.studiosedition.utilities.Alert;]]></mx:Script>
<mx:Button label="My Button" id="myButton" click="Alert.RecordEvent('Click');" mouseUp="Alert.RecordEvent('Up');" />
</mx:Application>That's it. I know this is a simple example, but the concepts are the same no matter how complex your code gets.





help
and feedback
latest
news
latest
forum entries