[ActionScript 3] Flashy Advertisements


Leeds

I think pretty much everyone thinks Flash is dead or dying and on the way out these days. Probably one of the last places where Flash survives, other than in Flash games or animation, is in advertising. Such are joys of this new industry.

In any case, over the past couple of weeks I’ve been collecting bits and pieces of ActionScript 3 to add to my repertoire. Given that probably no one really cares about this shit (excuse my French), I’ve combined a couple of things into the one post so you can just skip the rest if it doesn’t pertain to you.

My movieclips are buttons

It’s true - you can create button symbols/objects in Flash. But occasionally as a part of the structure of your advertisement, you may need to have a click action placed on a movieclip. It’s easy enough in these cases to addEventListener to give it a button’s functionality, but when you roll over this item with your mouse, a hand pointer doesn’t show up indicating that this is a clickable item. Thankfully, we can fix this with ActionScript.

mc.buttonMode = true;

Loading in an XML file

Something which seems to have become really popular since I joined is ‘feed advertisements’, which basically take either an XML file, a JSON file or data from an API and use this to fill in fields in an advertisement, whether this be product names, descriptions, headlines, images and so on. An exercise I’ve been struggling with for the past week is loading in an XML file and getting the data in it to populate some placeholder fields on my stage.

To load in an XML file we need to create an instance of the XML class, ensuring that we can only load in an XML file. Then we need to ensure that we have something to load the file in, i.e. the URLLoader class. Finally, we can use one of this class’ methods to load the file (in this case a local one, or a specific address).

var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("sampleXML.xml"));

Then we can add a listener to check that this file has been fully loaded up before we continue with processXML, where we actually load it into the XML variable.

myLoader.addEventListener(Event.COMPLETE, processXML);

So, time to load it into the instance we created earlier and ensure we have loaded in the right thing.

function processXML(e:Event):void {
  myXML = new XML(e.target.data);
  trace(myXML);
}

A lot of this XML stuff is still rather mysterious, and I’ve forgotten a lot from the subject I did a year ago now, but I found a great tutorial on Republic of Code that helped me really puzzle through the basics, then I could mess around and see what happened each time after that.