Sunday, December 03, 2006

User Events With Actionscript 3

I came across this problem the other day. I was looking all over for a simple article explaining how to create your own events with the new event system in AS3. I found a few interesting links, such as Darron Schall's blog post, but nothing completely definitive. Along with that article and some digging in the AS3 documentation I managed, amazingly, to successfully create my own custom event.

It's even easier to implement than when using AS2. First you need to create the actual event that will contain your custom event data. If you don't have any custom data you need to pass via the event then this is completely unnecessary to do. In my case I'm sending out the forecast for Miami Beach so one is needed.

The first step is to extend the built in event class.

package classes{


public class WeatherEvent extends Event{


Once we have the class definition created we need to define the type of event it will be. In my case I will be calling it update. There also needs to be a variable that will allow the event to contain custom data.

package classes{


public class WeatherEvent extends Event{

public var _message:String;
public static var UPDATE:String = "update";

Next the event needs a constructor. Don't forget to allow the ability for the custom data to be passed. The super constructor also needs to be called with the type of our event which is exactly why we defined it earlier.

package classes{


public class WeatherEvent extends Event{

public static var UPDATE:String = "update";
public var _message:String;

public function WeatherEvent($message:String){

_message = $message;

There is one last piece. There needs to be a clone method that will duplicate the event in the case that it's relayed. Check this link for more information.

Next up is the class that will dispatch the event in the first place. It's rather long and I wish to keep this short so I'm just going to paste it.

package classes{

import classes.WeatherEvent;

public class WeatherData extends EventDispatcher{

public var forecast:Object;

private var xmlData:XML;
private var loader:URLLoader;
private var rss:String;

static var instance:Object = null;

public function WeatherData($rss:String){

rss = $rss;



public function updateForecast():void{


loader = new URLLoader(new URLRequest(rss));

loader.addEventListener(Event.COMPLETE, parseData, false, 0, true);

loader.load(new URLRequest(rss));

private function parseData(evt:Event):void{

xmlData = new XML(;

forecast = xmlData..description[1];

dispatchEvent(new WeatherEvent(forecast));

public static function getInstance():Object{

if(instance != null){


return instance;

instance = new WeatherData("");

return instance;


As you can see in the parseData method dispatches a WeatherEvent object. Finally, to add a listener and see if it works! First to import the classes and create a WeatherData object.

import classes.WeatherData;
import classes.WeatherEvent;

var conditions:WeatherData = WeatherData.getInstance();

Then to create a function callback for the listener.

function checkUpdate(evt:Event):void{

trace("Forecast Updated");

Finally all that's left is to add a listener and force the WeatherData class to dispatch an event.

conditions.addEventListener(WeatherEvent.UPDATE, checkUpdate);


Ctrl+enter to test the movie and the output should look something like this.

Forecast Updated
Currently mostly cloudy and 66. Winds from the NNW at 9 mph. Relative Humidity at 73%.

Thursday, August 10, 2006

Using The Flex 2 SDK with Flash Develop

I saw Keith Peter's blog post about using FlashDevelop and the Flex 2 SDK.

However, there is one problem; insects really bother me. Especially ants. So here's a way to use the Flex 2 SDK without those pesky ants getting in the way.

First you need to download all the files needed.

The new ASCompletion DLL
The Flex2 SDK
Flash 9 Intrinsic Class Files
Flash Build Plugin

Download and unzip the new ASCompletion DLL into the Flash Develop plugin folder. Unzip the Flex2 SDK into "C:\Program Files\Adobe" . Once that's complete you'll need to download and unzip the Flash 9 Intrinsic class files into any folder you please (I recommend the base Flash Develop install folder). Finally, unzip the latest Flash Build plugin into the Flash Develop plugin folder.

With all this completed open up Flash Develop and create a new standard project. Flash Build may or may not ask you if you'd like to create a build file for this project. If it does press yes, if it doesn't it should have already created one called "flashbuild.xml" in the root folder for the project.

Once Flash Build has created the flashbuild.xml file double click on it to open it in flash develop. You will see a very nice GUI that will allow you to easily setup different builds for the project. Choose the ClassPath's item from the FlashBuild drop down list. You should see the default class paths there. However, in order to get Flash 9 functionality you need to add the AS3 Intrinsic classes that were unzipped earlier to the global class path. In my case this was "C:\Program Files\FlashDevelop\AS3_intrinsic_classes". Once that is done it's time to setup the actual build.

You should see an item in the FlashBuild list called "Build 1" select this item and on the right a new set of controls will appear. Choose a build name, I chose "AS3 Build", very original. Once that is done right click on the newly named build and choose "Add Flex2 Build".

Choose the newly created Flex 2 build by clicking on it. A new set of options will appear in the right hand pane with a few tabs: Quick Start, MetaData, and Advanced. If not already selected, choose the Quick Start tab. Once the QuickStart tab is selected add the main class file as the "Input MXML/AS". In my case, because I'm using the standard project template, this was "./classes/".

Alright, now you're ready to test the build. From the top menu choose the Build drop down. From that choose the Build item and then "AS3 Build" or whatever your build's name was. If all went successfully it should output something like below in the output window.

Running process: C:\Documents and Settings\Sam\My Documents\New Project\Flashbuild.bat 
"C:\Program Files\Adobe\Flex2SDK\bin\mxmlc.exe" "classes/" -accessible=false -allow-source-path-overlap=false -as3=true -benchmark=false -debug=false -default-background-color 13421823 -default-frame-rate 24 -es=false -generate-frame-loader=false -headless-server=true -incremental=false -keep-generated-actionscript=false -lazy-init=false -optimize=false -show-actionscript-warnings=true -show-binding-warnings=true -show-deprecation-warnings=true -strict=true -use-network=true -verbose-stacktraces=false -warnings=true
Loading configuration file C:\Program Files\Adobe\Flex2SDK\frameworks\flex-config.xml
C:\Documents and Settings\Sam\My Documents\New Project\classes\Main.swf (494 bytes)

Make sure your file is AS3 compliant. The bare bones should look like this:

public class Main{

public static function main():void{


Try to build your project again. If all goes well it should work flawlessly.

Congratulations on your ant free build.

Friday, August 04, 2006

Head First Design Patterns

Link:Head First Design Patterns

I always found patterns rather gimicky. I've tried to understand them with little success. I tried to read the infamous gang of four book (here) and ended up confused and bored. I've read and read the pattern section in Essential AS2.0 but can't stop but get the feeling of "why isn't he actually saying anything?".

At the bookstore the other day I was browsing the computer books section (very small where I live) and saw an O` Reilly book I hadn't seen before: Head First Design Patterns. It looks cheesy, and it was quicky discredited at first, until I actually started reading it. This book isn't for individuals with established knowledge about design patterns. It's a beginner book that shows that just because you know OO doesn't mean that you use it in a fashion that allows for functionality along with flexibility.

It's a real eye opener and not a boring read (it has pictures). Deffinately reccomend it to any one who just can't get patterns or see why they're useful.

Monday, July 31, 2006

PHPVars -- Handy LoadVars Accessory



I had to load itemized PHP data in a project I was working on and couldn't find any good solutions to easily load many items with PHP at once. Check the doc's usage to see what I mean by itemized data.

Wednesday, June 28, 2006

Flash 9 Pro Public Alpha!


This popped up first on digg of all places!

Get it while it's hot!
Flash 9 About Screen

Tuesday, June 27, 2006

Arrays Faster Than Objects?

I was talking with my friend Jarrad tonight and he pointed out that simply switching out an object for an array yeilded speed improvements. This got me thinking, is it true?

In my test it showed that objects are faster, but only slightly.

The test involves both an object and an array filled with seven values/properties all containing the same ammount of data. It then runs a ten-thousand iteration for loop and adds the same value to each index/property (both using a loop to weed out any inaccuracies).

The results are as followed:

Object Time: 1773ms
Array Time: 1977ms

Interesting :)

Sunday, June 11, 2006

SmurfLib Update-- Templates


I added two template classes to com.object.utils: StaticTemplate and Template.

StaticTemplate is unique because it transpose all the values changed within a template instance onto all templated objects.
For example:

import com.object.utils.StaticTemplate;

var obj1:Object = {};
var obj2:Object = {};

var sTemplate:StaticTemplate = new StaticTemplate({vector:new Point(10, 15)});


trace("o1.vector.x: "+obj1.vector.x); //10
sTemplate.vector.x = 20;

trace("o1.vector.x: "+obj1.vector.x); //20
trace("o2.vector.x: "+obj2.vector.x); //20

As you can see when the values in the template change all the instances that have the template applied change aswell.

The Template class only applies all the values to the templated object and changes to the template don't corrispond to the object templated.

import com.object.utils.Template;

var myTemplate:Template = new Template({lol:"LMAO"});

var myObj:Object = {};

myTemplate.applyTo(myObj); = "OMG";

trace(; //LMAO

Download and enjoy!