I have been creating a code base for a Screen Manager system that I am using for an iOS app I am building in AS3. I wanted to create a dynamic way to reference and instantiate classes that represent each of the Screens in the app so I can reuse this for other applications in the future and to make it a lot less code intensive.

I setup a series of static variables that held string names to represent each of the Screens in the application. Then when I want to create the screen and add it to the stage I use the getDefinitionByName() method and pass the string name of the class. Continue reading “getDefinitionByName”

Weak Listeners

Garbage collection can be a big deal when writing complex programs in Actionscript. Not removing EventListeners is probably one of the biggest sources of AS holding onto memory it doesn’t need.

There is a very easy way to allow GC to handle your event listener clean-up for you when creating a new event listener – the useWeakReference argument.

Here is some standard code we’ve all done million times when creating a button:

var button:MovieClip = new MovieClip();
button.addEventListener(MouseEvent.CLICK, click_handler_function);

The two arguments are pretty straight forward the first is the event we’re listing for and the second is the function that is called once the event is fired, but there are actually 5 arguments that can be passed to addEventListener() here is the method definition:

addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void

the 5th argument (useWeakReference) is false by default but by setting to true, this eventListener will be automatically removed and freed for Garbage Collection when required.

Here is the new eventListener code updated to make use of useWeakReference.

button.addEventListener(MouseEvent.CLICK, click_handler_function,false,0,true);

Castfire AS3 Library

I have been developing an AS3 library for the video publishing company Castfire on my own for the past little while and recently got in touch with them about continuing the endeavor. I thought it was a shot in the dark but they were more than willing to help out and so here is the first of, I hope, many articles on the Castfire API and the class library I am working with them to create for it.

The point of this library is to allow Flash devs like myself to easily access the Castfire API when creating custom video players.

Continue reading “Castfire AS3 Library”

WordPress and Flash

Typically we have always created Flash sites from scratch and used XML or, in the case of, a custom-made CMS as the content source

For the bulk of our clients having a custom-made CMS doesn’t work with their budget so we are left with XML. This is a great option for simple updates or if the user has some modicum of web savvy but for the average person it can be daunting to update.

I have dabbled in using Drupal and creating a DruFlash type of site but found that Drupal’s interface was not as user friendly as I would like for clients.

Personally I really like how WordPress is put together – it makes sense, is easy to use and the advanced features are there but not in the way. It also seems that people in the realm of marketing and PR have at least a little experience with it so in my opinion it was the obvious choice to tie into a Flash based site.

The Issues

The first issue I ran into was the most obvious – How do I get the content out of WordPress? The answer was much simpler than I was expecting. I found a pre-existing plug-in for WordPress called Flash API. It is not for the average user as it utilizes some custom PHP and a custom class in AS3 to pull the content out of WordPress.

This is a complete tangent but I learned a while ago that there are no gold stars for re-inventing the wheel when it comes to code. If someone else has taken the time to solve your problem for you – thank them (by giving credit where you can), expand on it if you can and move on.

The second issue I ran into was – getting around limited HTML tag support in Flash HTML textfields. This took a little bit of thinking. The first thing I did was figure out what WordPress used when making something bold, italics or a different colour and balance that with what options the client wanted.

Once I knew the code that WP created, I just used some basic regular expressions and string manipulations in Flash to change the tags to ones that Flash supported.

At that point I thought I was out of the woods as far as getting the content out of WP but after uploading to the testing server I found that there were times that the content didn’t load but it was very random.

I figured out that the issue lay in how often I was querying WP. The original plan was to pull the content for a given section only when I needed it and sometimes the Flash portion of the site was loading before the content was ready and due to the way the artwork and transitions worked I couldn’t drop an event listener in to wait for it. Long story short, the section would load without content. Which is not really good web design.

So after some thinking, I decided to revamp my AS3 class to pull all of the content at once in the root swf before the site loaded where I could control the load with event listeners and incorporate some error handling and fallback options.

The upside of doing this was that once the site pulled the content we were free of WP and any potential connection issues. The downside was that the whole site needed to be reloaded to see new content but that’s a small price to pay for the increased stability.

Overall I found the process to be great. There were times when I wanted to lose my sh*t over it but I can see the benefits of building a reusable code base for this use.

Going Somewhere?

Opening a URL in Action Script 3

Navigating to a URL in AS 2 was pretty straight forward.


As with most things in AS3 that has changed a fair amount. It is no longer one line of code. I’ll show you everything and then walk you through it Continue reading “Going Somewhere?”