Tag Archives: as3

Arrivals revisited

It’s been over two years since we (myself and Leif) created Arrivals.

At my last lecture, in which I talked about webservices, I took it out of the drawer to show my students. I was amazed that it still worked – offline that is. For some reason the images of the first destination (Paris) would not show.  So today I set out to fix that. The problem only beeing present on the first destination, I thought it had something to do whit load time, but still found that rather puzzling as I new the code would handle that. I looked at the XML returned from Flickr  and noticed that the farms that images from the first destination was located on, all had a rather hight id number, so I started to suspect the problem was related to the crossdomain policy issue. (This was made in as3) I was correctly loading a crossdomain.xml file from the Flickr site as such:

 for (var j:uint = 1; j<5; j++) {

Security.loadPolicyFile('http://farm'+j+'.static.flickr.com/crossdomain.xml');

}

Notice that the code is handling the fact that there is a crossdomain.xml for each farm on flickr, then to be on the safe side loads all 5 of them. Problem is this number increases over time – as Flickr adds more farms to host people’s images. Why oh why did I decide to hardcode the limit value (5)? Could it be that when I wrote the code I didn’t completely grasp why I had to load these policy files in the first place? I guess so. Anyway, looking at it today it took me NO TIME to se that I did not need to load ‘all’ (read 5) policy files for each farm – for each image. I only need to load the policy file for the farm on which the current image is located. And which farm I need to load with a given image, is in the data returned from Flickr: photo.@farm. So the loop was replaced with this single line:

 Security.loadPolicyFile('http://farm'+photo.@farm+'.static.flickr.com/crossdomain.xml');

And now the Arrivals app works again. Hurray!

Issue with document class shared between multiple assets.

Working on an suite of games for a client, I stumbled into a problem which I think is classic for AS3 developers doing stuff where a lot of external swf’s are loaded. If some of these swfs share the same document class, removing one instance of that class entirely from memory can prove to be a bit complex. I had to read up on garbage collection which Grant Skinner has an excellent article about.

Following the advice of Skinner,  I made sure my code was cleaning up after all unloaded assets (removing all listeners, nullifying, running the unload method on the loader) – still didn’t work.

Then finally I came to think of the ApplicationDomain and loaderContext property I’d provided for every new load call:

var ldrContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
loader.load(mRequest, ldrContext);


Which meant that all movies where beeing loaded into the same application domain, and by default – if you ommit the loaderContext line and  your load command looks like this:
loader.load(mRequest)

everything is loaded into it’s own ApplicationDomain presumably, and you don’t get any of the malfunctions that comes with virtually different objects sharing the same class. After I removed the loaderContext it worked.

So I’d been on a serious deroute with all my clean up code, but I guess I should be grateful this problem forced me to create some tidy up code. Surely it has eliminated bugs that I wouldn’t have found in my hand made tests, but that would have been bound to occur  when my end users start loading and unloading games in the chaotic manner that end users always do…