Blog blurb

This blog is the resting place of my various projects. It's a place to find out about my various commercial and personal projects. Some of them are quite geeky and some of them are more arts based. All my projects are a small subset of all my ideas for projects. There's not enought time really.

RSS Get updates by subscribing to the RSS feed.

Richard's weather

cloudy

Richard in Leeds is mainly experiencing medium cloud level.

ActionScript 3 timeout problems when working with XML

06/01/2008

I was getting the following error while developing my doodle flash application using ActionScript 3.0:

1502 A script has executed for longer than the default timeout period of 15 seconds.

I tracked the problem down to a piece of code that looped through a large XML document. At first I thought there was nothing I could do, but after playing about with it and pulling my hair out for a while I found out several things that helped me reduce the runtime by an impressive amount. I wanted to share my findings in case anyone else was having similar problems.

Finding 1: XML .. operator is very slow

ActionScript 3's new XML notation is fantastic, but I got a bit carried away with the .. operator, also known as the descendant accessor.

The descendant accessor returns a collection of all nodes in an XML node that have a specified name. It looks a bit like this:

filmDirectors = filmCollectionXML..director;

In this example filmDirectors now holds a collection of all director nodes. The .. operator searches through all filmCollectionXML's nodes and their child nodes for any node called director.

If you need to search for stuff this is great, but I was using it as shorthand when I knew where the nodes were, but they were quite deep in the XML.

I ran some tests and found that in a 250 node XML document using the descendant accessor to get a property took on average 0.7ms, where as accessing it directly took 0.2ms. That's 3.5 times as much time and when you're looping through an XML document, this can become a big deal. Also, using .. gets slower, the bigger the XML document is. Getting rid of .. from my XML loop fixed my crashing problem.

So, don't use the descendant accessor as shorthand. Only use it if you have a good reason to.

Finding 2: Functions in a loop condition are run on every iteration

This is obvious really, but it's easy to overlook. I had a loop header that looked a bit like this:

for(var i:Number = 0; i < filmCollectionXML..director.length(); i++)

The loop condition 'i < filmCollectionXML..director.length()' has two functions in it; .. and length(). These two functions will run on every iteration of the loop. The following will run a lot quicker:

var numDirectors = filmCollectionXML..director.length();
for(var i:Number = 0; i < numDirectors; i++)

And, it would be even quicker if we didn't need to use the .. notation at all, but using it just once is okay.

So, where possible, pre-compute the value of functions needed in loop conditions.

Finding 3: Things get slower the bigger the XML document is

If you're looping though a whole XML document then this will obviously take longer if the document is bigger. However, it's not so obvious that even accessing a single element of an XML document will take longer and longer the longer the XML document gets.

I ran some tests for the following operations:

  1. Accessing first element of collection: filmsXML.film[0];
  2. Accessing whole collection: filmsXML.film;
  3. Accessing length() of collection: filmsXML.film.length();

I found that they all took the same amount of time, but that the time to do them increased as the XML document got bigger. I did the same using the descendant accessor and found that it was even worse.

Time in milliseconds to perform XML operations
Number of XML nodes500100015002000
An XML operation0.40.91.31.7
An XML .. operation1.22.43.64.7

This graph shows it more clearly and how the time taken when using the descendant accessor increases at a quicker rate.

Graph showing how the time to do XML increases

So, try to limit the size of your XML documents and break them down into smaller ones if they're very large.

By using these three findings I was able to reduce the execution time from over 15 seconds to just over a second. I was very pleased.

RichardGComments: 0

Draw doodles on Flickr photo

22/12/2007

Doodle of a snowman on a flickr photo

This is my latest creation using Flash and ActionScript 3. You can draw a lovely doodle on one of your Flickr photos and then save it and send it to your friends.

It started as a simple doodle program, but I thought adding Flickr photos would make it more fun. It uses the Flickr API to load the photos and a web service I've written in ASP.NET to save and load the doodles.

I've been sending people Christmas doodles this year as Christmas eCards.

So, why not go and doodle on your photos and send them to your friends.

RichardGComments: 0

Getting the cursor hand to appear over MovieClips containing text in Flash

07/12/2007

I've been making an animated button using a MovieClip in Flash using ActionScript 3. The problem I had was making the hand cursor appear over it, firstly at all and then when my button had text in it.

To make a MovieClip behave like a button and use the hand cursor set the following properties for the MovieClip.

clip.buttonMode = true;
clip.useHandCursor = true;

Next, if your MovieClip contains a TextField set the following property for the TextField.

clip.labelText.mouseEnabled = false;

This means that the button underneath deals with the mouse, so the cursor will behave in the way that the button tells it to. In the example in this post, Button 1 has the mouseEnabled set to the default true value and Button 2 sets it to false.

I have an ActionScript Class file linked to the button that does all this to keep my main movie code clean. 

RichardGComments: 0

Flash Flickr viewer

16/11/2007

This Flickr viewer uses Flash CS3, ActionScript 3 and the Flickr API to display public photos from a selected user's Flickr account. I've created it because I've got a few project ideas where you will need to select a photo from Flickr. I've been impressed with the improved object orientedness of ActionScript 3 and this has let me build this in a way where all the different bits can be re-used very easily.

This has been a nice project to get my head round some of the new features in ActionScript 3. I've been particularly impressed with the new way of loading and using XML. It's so much easier and just makes sense. The event model has also been much improved.

I'm also very impressed with the Flickr API. It's great to be able to take the wealth of content they've got and play about with it. In the future I hope to play about with some other APIs. I love how sites are offering APIs and letting people extend and join up their services. I think the joined up web offers some amazing possibilities.

RichardGComments: 1