Flash Tutorial: Displaying Tweets in Flash
Using XML data from a Twitter feed, it is very easy to set up a Flash app to simply display your latest Tweet. With Twitter becoming an ever more powerful tool to spread information with, it has never been more important to display Tweets on a website. Now you will be able to do it through Flash.
Step 1: Prepare the Background
Lets start out by designing a simple background for your Tweets to sit in. Include some text and an icon to show where users could click to view the whole of your Twitter feed.
Here’s the image I used for my background. The PSD can be found with the source files. Feel free to use this or create one of your own. The dimensions we will be using for this Flash file will be 500 x 425.

Step 2: Setting Up the Main Flash File the Follow Button
OK, so we’re going to open up a new Flash document and be using Actionscript 3.

Once that’s open set the stage dimensions to 500 x 425, and then import your lovely background onto the centre of the stage.
We now need to turn the text where you said we could go to your Twitter page, actually into a button.
So lets start by creating a new layer, Insert > Timeline > Layer. Then using the rectangle tool draw a rectangle of where you want your button to be. Convert this shape into a Symbol > Button and turn the alpha down to 0%. The button should now be invisible and look something like this.

If you preview the Flash document now you should see the mouse turn into a hand where the button is, but nothing will happen when you click on it.
Step 3: Adding Some Actions to the ‘Follow’ Button
Give the new button an instance name of ‘follow’ and then create a new layer for our actionscript to sit on. Within this new layer open up the Actionscript panel and input this:
1 2 3 4 5 6 7 | function subscribeLink(event:MouseEvent):void { //Insert your own Twitter Link var URL:URLRequest = new URLRequest("http://twitter.com/xuroqflash"); navigateToURL(URL); } follow.addEventListener(MouseEvent.CLICK, subscribeLink); |
This creates a new function that will navigate to the Twitter page and an event listener that will check to see when the button has been clicked.
Preview you flash file now (Ctrl/Mac + Enter) and when the invisible button is clicked, it should take you to the desired web page.
Step 4: Setting Up Where Your Tweets Will Sit
Now to get down to what this tutorial is really about, displaying your latest Tweet in Flash. First up, lets create another new layer. Then create a text box where you want your latest Tweet to sit. Within the text box write something along the lines of ‘Loading Twitter Status’. This text will be displayed while your Twitter post is loading. Feel free to customise the font type, colour and size.
You should now have something like this:

Give the text box you just created an instance name of ‘twitter_txt’.
Step 5: PHP in a Flash Tutorial?
Yes unfortunately so. Due to the security restrictions in Flash it is not possible to grab data from an external domain, so you have do use a PHP file to do it for you. Simply create a new PHP file in your favourite text editor and input this code, replacing ‘xuroqflash’ with your Twitter ID:
1 2 3 4 5 6 |
Call the file ‘latest-tweets-in-flash.php’, and save it in the same directory as your .Fla file. There is also a PHP file located in the source files if you are having trouble.
Step 6: Starting the Actionscript File to Show Tweets
We are going to put all the Actionscript code for the Tweets in its own file. So go ahead and create a new Actionscript document.

Now for the code. First off we need to start the package and import all the classes that we will be using. The package keyword allows you to organize your code into groups that can be imported by other scripts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package { import flash.display.MovieClip; import flash.events.Event; import flash.net.URLLoader; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.net.URLLoader; import flash.net.URLRequest; import flash.events.Event; import flash.events.MouseEvent; import flash.net.navigateToURL; public class Main extends MovieClip { } } |
Step 7: Loading the Twitter XML Data
Now we need to grab the Twitter feed data from the PHP file we created before. So we need to set up a new loader function to locate the PHP file and attach the XML to the Flash file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | package { import flash.display.MovieClip; import flash.events.Event; import flash.net.URLLoader; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.net.URLLoader; import flash.net.URLRequest; import flash.events.Event; import flash.events.MouseEvent; import flash.net.navigateToURL; public class Main extends MovieClip { private var twitterXML:XML; public function Main() { loadTwitterXML("latest-tweets-in-flash.php"); } private function loadTwitterXML(URL:String):void { var urlLoader:URLLoader = new URLLoader(); urlLoader.addEventListener(Event.COMPLETE, finishLoadingXML); urlLoader.load(new URLRequest(URL)); } } } |
Step 8: Parse the Twitter XML and Send it to the Text Box
Now we need to set up two new functions that will find the correct Twitter data and then display it in the text box we created earlier. So first up we tell it what to do when the Twitter feed has loaded, which in turn starts another function that will show the Twitter status in the ‘twitter_txt’ text box.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | package { package { import flash.display.MovieClip; import flash.events.Event; import flash.net.URLLoader; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.net.URLLoader; import flash.net.URLRequest; import flash.events.Event; import flash.events.MouseEvent; import flash.net.navigateToURL; public class Main extends MovieClip { private var twitterXML:XML; public function Main() { loadTwitterXML("latest-tweets-in-flash.php"); } private function loadTwitterXML(URL:String):void { var urlLoader:URLLoader = new URLLoader(); urlLoader.addEventListener(Event.COMPLETE, finishLoadingXML); urlLoader.load(new URLRequest(URL)); } private function finishLoadingXML(e:Event = null):void { e.target.removeEventListener(Event.COMPLETE, finishLoadingXML); twitterXML = new XML(e.target.data); showTwitterStatus(); } private function showTwitterStatus():void { twitter_txt.wordWrap = true; twitter_txt.autoSize = TextFieldAutoSize.LEFT; twitter_txt.htmlText = twitterXML.status.text[0]; } } } |
That’s all the coding done. Go back to the .Fla file and in the Properties Panel add “main” in the Class field to make this the Document Class.
Conclusion
Preview your Flash file (Ctrl/Mac + Enter). Your Tweets may not be displayed on your local computer due to the fact that the PHP will not run. However upload the .Swf file that will have just been created by you previewing the file, along with the PHP file to your server and all should now be working.
I hoped you learned a few tricks from this tutorial. See you next time for another Flash tutorial from XuroqFlash.








5:11 pm on February 9th, 2010
I just builded one but i didnt use PHP, for me that is useless for this tutorial you can just load the XML with flash, about the security, if you upload it to a server it won’t give you any problem and for you to see it working on ur comeputer you can just change the setting of the flash player, letting your swf or folder load external data
6:21 pm on March 2nd, 2010
Hello – Thanks for the tutorial and I have downloaded your source files. I keep getting this error though:
TypeError: Error #2007: Parameter text must be non-null.
at flash.text::TextField/set htmlText()
at Main/showTwitterStatus()
at Main/finishLoadingXML()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
What could be causing that? Thanks
5:30 pm on March 3rd, 2010
If you have followed the tutorial it should all work fine. I am not sure what is causing that error
5:37 pm on March 3rd, 2010
Does the file not run locally? I haven’t done anything to the source files, just tried to run it and I get those errors in the output panel.
2:50 pm on March 4th, 2010
My apologies, it doesn’t work locally! I have to stop skimming over tutorials! Thanks for this tutorial, greatly appreciated.
4:11 pm on March 5th, 2010
I do however have one quick question. Is there way to also display the time & date of each tweet? Thanks.
8:58 pm on March 7th, 2010
yes, there is actually one way, you just have to create another text field and assign the sapce called pubDate from the XML, and that’s it!