Using AS3 and XML to Define a getURL Button
Posted on May 20th, 2009So let’s say you have a Flash site using the very cool and very popular “swfaddress” deep linking technology but you want to define where the links go using an XML file. This is simpler than it might sound, but beneficial for lots of reasons.
For instance, we are currently working with a client that records the progress of a user’s journey through their web site into an XML file. This means that as the user chooses to go to “this” page “that” page is no longer available but “this new page” is available. Almost a “choose your own adventure” type of Flash web site. So they have their XML file being made as the user clicks through, but the links ON the Flash site need to reflect what the XML file is saying.
So, for example, the user has a choice to click “Page 1,” “Page 2,” or “Page 3.” They choose “Page 2.” That needs to remove “Page 3″ from their choices on the next page while keeping the navigation buttons the same. Enter this code.
Now the XML can say, “button three, you’re going to link to page 3 on this page.” Then, after the user goes to page 2 it can say, “now you’re going to link to page 5,” or whatever.
Pretty cool idea huh?
Plus… since we’re using the swfaddress deep linking the user will always be able to go “back” and “forward” (along the choices they’ve made) as they’re using the site. Even more powerful.
Here’s what the XML file looks like.
(based on the Kirupa XML tutorial)
<?xml version=’1.0′ encoding=’UTF-8′ standalone=’yes’?>
<Books>
<Book ISBN=”0553212419″>
<title>Sherlock Holmes: Complete Novels</title>
<author>Sir Arthur Conan Doyle</author>
</Book>
<Book ISBN=”0743273567″>
<title>The Great Gatsby</title>
<author>F. Scott Fitzgerald</author>
</Book>
<Book ISBN=”0684826976″>
<title>Undaunted Courage</title>
<author>Stephen E. Ambrose</author>
</Book>
<Book ISBN=”0743203178″>
<title>Nothing Like It In the World</title>
<author>Stephen E. Ambrose</author>
</Book><Navigation>
<PreviousButton></PreviousButton>
<NextButton url=”http://www.yourwebsiteurl/#/frame_10/” name=”name”></NextButton>
</Navigation>
</Books>
Here’s what the actionscript for the button looks like.
// BUTTON
navNext.addEventListener(MouseEvent.CLICK, nextClicked);function nextClicked(e:MouseEvent):void {
var xmlNextLoader:URLLoader = new URLLoader();
var xmlNextData:XML = new XML();xmlNextLoader.addEventListener(Event.COMPLETE, LoadNextXML);
xmlNextLoader.load(new URLRequest(”story.xml”));function LoadNextXML(e:Event):void {
xmlNextData=new XML(e.target.data);
ParseLinks(xmlNextData);
}function ParseLinks(linkInput:XML):void {
var linkAttributes:XMLList=linkInput.Navigation.NextButton.attribute(”url”);
for each (var urlLink:XML in linkAttributes) {
trace(urlLink);
var request:URLRequest=new URLRequest(urlLink);
try {
navigateToURL(request, ‘_self’);
} catch (e:Error) {
trace(”Error occurred!”);
}
}
}
}
And finally… you can download the source code here.
(it’s done in CS4 / AS3)







Recent Comments