Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 10. Working with an XML Playlist > Creating hyperlinks using XML data

Creating hyperlinks using XML data

The XML data that you loaded into the Lesson 10 file contains a link element with a URL for the location of each song on iTunes. The link_btn element in the upper-right corner of the Flash Stage will use ActionScript’s navigateToURL() method with the link element’s URL data to create a hyperlink for each song. In Lesson 1, “Using Code Snippets and Navigating the Flash Timeline,” you used a code snippet to create a link using the navigateToURL() method. Now you will write the code yourself.

1.
In the Actions panel for Frame 1 of the lesson10_start.fla file, locate the addEventListener() methods in the xmlLoaded() function.

2.
On a line below the existing addEventListener() methods for the six song clips, insert a new addEventListener() for link_btn with the following code:

link_btn.addEventListener(MouseEvent.CLICK, iTunesLink);

When the link_btn element is clicked, it will call a function called iTunesLink(), which you will now create.

3.
Scroll to the end of the code for Frame 1 and, below all the existing code, add the shell for the iTunesLink() function:

function iTunesLink(e:MouseEvent):void {
}

You access the iTunes element of the XML data similarly to the way you accessed the name, artist, and file elements.

4.
Within the curly braces of the iTunesLink() function, add this line:

var link:String = songList_XML.song[songNum + songCount].itunes;

Which itunes element from the XML data will be used is determined by adding the number stored in the songNum variable to the number in the songCount variable. Recall that the switch statement sets the songNum variable based on the song clip that the user selected. Up until this point songCount equals 0, so the link element will match the song clip that was selected (you will work more with songCount soon).

Once the appropriate link from the XML data has been stored in the link variable, you can use that variable in a navigateToURL() method to create the actual hyperlink.

5.
On a line above the closing curly brace of the iTunesLink() function, add the following code:

navigateToURL(new URLRequest(link), "_blank");

This code will open the URL stored in the link variable, and the link itself will launch iTunes and go to the appropriate location. The full function should read:

function iTunesLink(e:MouseEvent):void {
 var link:String = songList_XML.song[songNum + songCount].itunes;
 navigateToURL(new URLRequest(link), "_blank");
}

6.
Test the movie. Select a song and then click the iTunes link. If iTunes is installed on your machine, it should open and automatically navigate to the page for the song you selected, otherwise you will be taken to this song on Apple’s iTunes website.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint