This is Ask the Editors; your chance to fire questions at Michael and me and our chance to help you out directly.
In this post we'll be looking at looping through XML, array syntax, mp3 players, Flash CS5.5 plus much more. Read on for loads of Active answers and find out how to tell us what's foxing you…
Thanks for all Your Questions!
The Activetuts+ inbox filled up pretty quickly and below you'll find answers to a pile of your questions. Disagree with something we've said? We'd love to hear your opinions in the comments! (As if you needed telling...)
John Doe:
QI'm having trouble figuring out how to loop through an XML feed that has my RSS feed and display the nodes that I want to show. I would love to see how I can show all or just the first 3 or 5.
Michael James Williams:
Hey John (if that is your real name)!
Let me give readers a little context, first. RSS feeds are XML documents with this format:
<?xml version="1.0" encoding="UTF-8" ?> <rss version="2.0"> <channel> <title>Name of channel (e.g. Activetuts+)</title> <link>URL to website (e.g. http://active.tutsplus.com)</link> <description>Description of channel (e.g. Browser-Based RIA and Game Development Tutorials)</description> </channel> <item> <title>Title of one item in the feed</title> <link>URL to that item's full page</link> <description>Synopsis of item</description> <author>Author of item</author> </item> <item> <title>Title of one item in the feed</title> <link>URL to that item's full page</link> <description>Synopsis of item</description> <author>Author of item</author> </item> </rss>
An RSS feed will only have one <channel> element, but can have many <item> elements. Also, the channel might have more child elements than I've used in this example, and each item might have more or less items than I've used.
Once you've got this document into an XML object using a URLLoader
, you can use E4X to access fields matching certain criteria.
For example, to get the very first <item> node, you reference:
var itemNode = rssXml.item[0];
Just as in an Array, the 0 says "get me the first element". You can then access the sub-nodes of this <item> like so:
trace(rssXml.item[0].title); trace(rssXml.item[0].link); trace(rssXml.item[0].description); trace(rssXml.item[0].author);
To get the first three items, use a for
loop:
for (var i:int = 0; i < 3; i++) { trace(rssXml.item[i].title); trace(rssXml.item[i].link); trace(rssXml.item[i].description); trace(rssXml.item[i].author); }
Watch out, though; if there aren't at least three items in the feed, you'll get an error. You can check how many items are in the feed using rssXml.item.length()
. Note that, unlike an Array, length()
is a method here, not a property -- it has parentheses.
This means that you can loop through all items in the feed like so:
for (var i:int = 0; i < rssXml.item.length(); i++) { trace(rssXml.item[i].title); trace(rssXml.item[i].link); trace(rssXml.item[i].description); trace(rssXml.item[i].author); }
I hope that helps! You may need to tailor it to your specific feed if it's not formatted in the same way as my example, but this should give you the basics.
For a full explanation of XML and E4X, check out AS3 101: XML, by Dru Kepple.
Vjeran:
QCan you explain the difference between,
var ar:Array = new Array();
and
var ar:Array = [];
?
Michael James Williams:
Hi Vjeran! Good question.
There's not a huge difference between those two statements; each one will create a new Array with the name ar, containing zero elements -- i.e. ar.length
will return 0
. The difference is in the details.
First, I've seen tests that show that, in Flash Player 9 at least, the second statement takes slightly less time to run. We're talking milliseconds, or even fractions of milliseconds, but if you're writing processor-intensive code then every bit helps. I'm not sure if this is still true in the latest versions of Flash Player, however.
Second, take a look at these two statements:
var ar:Array = new Array(10, 17, 92, 100);
var ar:Array = [10, 17, 92, 100];
These two lines do exactly the same thing as each other: they create an Array called ar
which contains four elements: the first is 10, the second is 17, the third is 92, and the fourth is 100.
However, compare these two statements:
var ar:Array = new Array(10);
var ar:Array = [10];
This time, the second statement creates an Array called ar
with one element: 10. But the first statement creates an Array called ar
with ten elements, each of them empty.
Those are the only differences I know.
Steven:
QI am in the last legs of finishing a website and my family friend throws me a curve. Nothing new. Except I have never put an audio player in any of my sites before. The friend wants me to create a player that can play up to three radio interviews he did for a book he published.
Can you guide me to a simple and elegant method of accomplishing this request? Appreciate any help/advice you can give.
And thanks for the last years worth of greatness! I became a premium member 2 years ago but am truly appreciating your product more as the time passes.
Ian Yates:
If you're looking at embedding music into a website, ActiveDen has almost 400 MP3 players on offer. Here are just a couple:
- Dynamic Mp3 Player (xml): AS2, XML driven, supports multiple categories, multiple tracks and has racked up over 1,100 sales which says something of its value.
- xml mp3 player - pi 1.0: This one is an AS3 player (if you're interested in rummaging around in the source), is also customisable via XML and wins points for its full, compact and "pill" modes. Very neat.
- Decent player: If it's simplicity you're after, this might be the one for you. List all three of your interview tracks in the XML file, then use the skip button to loop through them. Straight forward and cheap as chips.
Alternatively, if you're interested in pulling a Flash audio player together yourself, here are a couple of relevant tuts we've featured on the site in the past:
- Go Retro With a Funky Flash Music Player
- Create a Simple and Attractive MP3 Player
- Create a Jazzy XML Driven MP3 Player
- Create a Drag and Drop MP3 Player with Adobe AIR
One last resource, totally useless in your situation, but nice for marketplace users :)
If you need more specific help, feel free to shoot us an email: active[at]tutsplus[dot]com. Good luck with your site!
Mihai Ghelmegeanu:
QAre you planning an extensive coverage of the new Flash CS 5.5 features for mobile development and if so, how soon?
Ian Yates:
We're definitely looking at covering the new features available to Flash CS5.5 users. From intial feedback it doesn't seem that there are many of you out there yet, but there are plenty of folk just itching to dive into the the multi-device potential Flash CS5.5 offers (did someone mention iOS?). Aside from the developments in mobile deployment, there are also some great new workflow features and a pile of new improvements for working with the Flash timeline which I really like the look of.
New releases aren't always popular, but it's not our job to tell people they should or shouldn't be investing in x,y or z, we're here to inform our readers about what's available to them.
Luckily, we have some fantastic writers on board at the moment, developers who spend their working lives right on the front line, so you can expect some top-notch Flash CS5.5 content very soon.
Peter Eeckelaert (on Facebook):
QIsn't it best to only write getters and setters with a _ in advance (mainly just for reference recognition later on, so you directly know what the getters & setters are)?
Michael James Williams:
Hi Peter -- nice to see an Activetuts+ writer getting involved!
Yes, that's definitely one option. We talked briefly about using underscores as prefixes in this Open Mike discussion, but at the time I only suggested either using them for all private and protected variables, all variables entirely, or no variables. It never occured to me to use them just for private and protected variables that have a getter and a setter.
That's a smart idea; I like it! It does mean that you can't tell at a glance whether a variable without an underscore prefix is private, protected, or public, but if you're using a decent code editor (like FlashDevelop) it should let you know. Yours has the added advantage, then, that you can tell whether a variable you're referencing has a public setter or getter that you need to be wary of. Neat.
Ben Chabs:
QDo CMS for ActionScript exist? Do they work? If so, would it be a good thing for a company to start their own CMS or for a freelancer to create their own CMS to produce Flash websites?
Michael James Williams:
Hi Ben :)
Content Management Systems for Flash certainly do exist. I can think of two off the top of my head:
- Frame, by Neil Nand, which can also generate and manage HTML and mobile versions of the Flash site. I saw Neil present this at Flash Camp Birmingham earlier in the year.
- Modularweb CMS, by Björn Crüger of Piecemaker fame.
Unfortunately neither of these have been released to the public yet, but the point is that Flash CMSs are out there.
However... in my opinion, I would not recommend you create a new one right now. Why? Because I believe full Flash websites are on the way out. If you want to use a CMS, grab one of the great ones that have already been built by someone else; there's no point jumping on board now.
Ian Yates:
Adding to what Michael said, Moto CMS is another example of a Flash CMS and we ran a tutorial on basic usage a couple of months back. ModularWeb's CMS is now available and under continual development. Björn might well write up some kind of review for Activetuts+ so keep your eyes peeled!
Opinion about Flash CMS is mixed and usually falls into one of two camps: love Flash CMS or hate Flash CMS. A bit like vegemite. As far as Activetuts+ is concerened, we won't be giving it a great deal of attention as the future of Flash doesn't lie in website design.
Didn't See Your Question?
Sorry about that, we had a ton of questions to work through and there's still a chance it might appear in the next Ask the Editors post. If you’d like to submit another question for the next time around, do so using the form below:
Comments