Apr 26

Well that didn’t take long, Adobe posted the latest Flash Player penetration rates and 84% of users in the US and Canada are now equipped with Flash Player 9. In the past it usually took 2 years to reach 80% penetration, it only took 10 months for Flash Player 9. Amazing.This is the first time we will actually be able to publish something with the latest version of the Flash IDE and be confident that most people will be able to see our “stuff”.

Great work Adobe, and take that Microsoft.

Tags: , ,

Jul 10

I finally got around to really playing with Actionscript 3 and two words come to mind LOVE IT. Especially the new XML Class which is based on E4X, or true integration of XML in Actionscript. Gordon Smith said it best “Compared with the current XML support in the Flash Player, E4X allows you to write less code and execute it faster because more processing can be done at the native speed of C++.”

After playing with it and doing a few benchmark tests I noticed it wasn’t any faster than the XML object of AS 2.0. Maybe the E4X support isn’t fully optimized in the Flash 9 beta, really this shouldn’t matter because Flash Player 9 is final. But really, who cares, we’re talking milliseconds here, it’s like trying to figure out if you or your friend blink faster. The real bonus of E4X is its simplicity and query like abilities. No longer will we have to depend on an XPath implementation in Actionscript to query xml data. XPath was very slow and very processor intensive with it’s multitude of nested loops. I have seen XPath take down many of browser in my day.

Enough reminiscing, lets get back to E4X. XML nodes can now be drilled down using dot notation, just like a regular object in actionscript. There is also an attribute identifier operator (@). Take a look at the following simple example:

var myXML:XML =
<items>
     <item id="RedButtons" width="300">
          <label>My Red Label</label>
     </item>
     <item id="BlueButtons" width="200">
          <label>My Blue Label</label>
     </item>
     <item id="GreenButtons" width="250">
          <label>My Green Label</label>
     </item>
     <item id="RedButtons" width="400">
          <label>My Other Red Label</label>
     </item>
</items>;

//an XMLList is a subset of XML data
var myList:XMLList = myXML.item.(@id == "RedButtons");

//the new way of writing for each loops
var count:int = 0;
for each (p in myList) {
     trace(' - ' + myList[count].@width);
     count++;
}

//or a standard for loop
var len:Number = myList.length();
for (var i:int = 0; i<len; i++) {
     trace(myList[i].label + ' - ' + myList[i].@width);
}

So, what’s the first thing you noticed. That’s right, no more firstChild.childNodes, etc. Thank goodness! Finally an XML object is treated like a regular object.

As you can see the new XML class is called XML, the AS 1/2 XML class is now called XMLDocument. If you attempt to use legacy code and you compile to AS 3.0 make sure you update the class name first.

In the real world you would probably load in dynamic XML, but you will notice I simply typed in a simple snippet of XML and the compiler didn’t puke. Handy! So my first query which returns an XMLList (another new class) which contains only the item nodes with an id attribute that equals RedButtons. Very similar to XPath. But much quicker because it is native to the player now. Using my XMLList, that I so creativly named myList, I perform two seperate loops. A for each loop and a standard for loop. A couple things here you’ll notice, the syntax on the for each loop has changed slightly. Previously you simply wrote:

for (p in myList) {
     trace(p + ':'  + myList[p]);
}

With AS 3.0 you now have to add in the word each.

In my standard loop you will notice I have typed my index (i) as an int. It is good to use an int when you don’t need a floating point number such as a loop index. You would think this would increase performance, but really it doesn’t. You will also notice my len var equals myList.length(). length is a method so make sure you remember the brackets.

Inside both loops all I am doing is tracing out data. The only thing special here is how I access the attributes using the @ operator.

Well there you have it. Very simple example I know. I’m not sure which I am more excited about, this or the Regular Expression support in AS 3.0. Either way, I can’t wait for my clients to start letting me use it!

Tags: , , , , ,