ActionScript
Flash AS3: Processing RSS Feeds – Part 2
March 28, 2010
0

This post continues the topic of reading and parsing RSS2 feeds which were discussed in the previous post and can be found here: https://permadi.com/blog/2010/03/flash-as3-processing-rss-feeds/.

Building from the example in that post, we will do the following improvements here:

  • Cycling though posts.
  • Enabling users to read the full post.

To begin with, we added two buttons into the Flash movie:

  • nextButton to enable cycling through posts
  • readFullPostButton to enable opening the full post in a new browser window.

The buttons are shown below:

They are regular Button (can also be MovieClip) and we are giving their instance the name of nextButton and readFullPostButton respectively.

Attaching Listeners To The Buttons

Add the two new lines (line 21 and 22) to intercept buttons click. For this example, I am adding it at the end of onXMLFileLoaded function. I also hid the buttons until the RSS XML data has been loaded (line 2 and line 3).


// Hide the buttons for now until the RSS data is loaded.  We don't want user clicking on them before the RSS is ready.
nextButton.visible=false;
readFullPostButton.visible=false;

/**
* Called when the XML data has been loaded.
*/
function onXMLFileLoaded(e:Event):void
{
	myXML=new XML(e.target.data);
	//description.text=String(myXML.toXMLString());
	currentPost=0;
	numberOfPosts=myXML.channel.item.length();
	displayPost();

	// Make the buttons visible
	nextButton.visible=true;
	readFullPostButton.visible=true;

	// Add listeners to the buttons
	nextButton.addEventListener(MouseEvent.CLICK, onNextButtonClicked);
	readFullPostButton.addEventListener(MouseEvent.CLICK, onReadFullPostButtonClicked);
}

Handling the nextButton

This button needs to know how many posts are in the RSS feed, so that we can cycle back when user is in the last post. We stored this variable earlier in numberOfPosts variable.

function onNextButtonClicked(event:MouseEvent):void
{
	// Increment post index
	currentPost++;

	// Cycle back to the first post if we are at the last index
	if (currentPost>=numberOfPosts)
		currentPost=0;
	displayPost();
}

readFullPostButton

This button just needs to open a new browser window and displays the full post and lucky for us, the URL of the full post is already available in the RSS, within myXML.channel.item[currentPost].link node.  navigateToURL is used toopen the post in a blank browser window.

function onReadFullPostButtonClicked(event:MouseEvent):void
{
	navigateToURL(new URLRequest(myXML.channel.item[currentPost].link), "_blank");
}

 

Example

You can download the example FLA here.