ActionScript 3 Personal Cheat Sheet

Every once in a while I get asked to do Flash animations, and I love it, as this adds veriaty to my workload. However, I always forget some basic ActionScript 3 code and have to turn to google to figure it out. So I am going to use this blog post as my personal cheet-sheet and add stuff to it as I figure it out.

ActionScript 3 click event

//Register click event on an object (movie, button etc)
mc_clip.addEventListener(MouseEvent.CLICK, clickHandler);
//Execute code
function clickHandler(evt:Object):void{
    //do something
}

ActionScript 3 pausing the main timeline

//stop the playhead from moving forward
this.stop();
//create Timer object
var timelinePause:Timer = new Timer(2000, 1);
//tell flash what to do after two seconds are up
timelinePause.addEventListener(TimerEvent.TIMER, timerHandler);
timelinePause.start();

//execute "what to do"/continue playing
function timerHandler(evt:Object):void{
   this.play();
}

ActionScript 3 load and read XML file

//load XML file
var xml:XML = new XML();
var loader:URLLoader = new URLLoader();
loader.load(new URLRequest("some_xml_file.xml"));
loader.addEventListener(Event.COMPLETE, function(evt:Event):void{
   xml = XML(evt.target.data);
}

To load specific elements of the XML file

//use array access operator []
xml.element_name[0];

To laod a specific attribute

//precede an attribute's name with @ symbol
xml.element_name[0].@attribute_name

To find out number of specific elements

//use XMLList method length()
xml.element_name.length();

Related Posts:

  • No Related Posts