Tweeter v1.0 – simplifying Twitter and Actionscript

If you want to add a Twitter feed to your flash website, currently the best way to do it is to pay a visit to Matt Pearson’s (Zenbullets) Twitter API explanation and buckle down to the code. However if like me you are finding your clients increasingly requesting Twitter feeds in their sites, perhaps you’ll appreciate my new Tweeter class for ease of re-use.
Try it out:
It’s based around Matt’s explanations and centres on drawing a single tweet from a single user (using the API’s “status” call). All I’ve done in my clumsy ham-fisted way is to simplify it for anyone who either doesn’t have the time to learn it or simply can’t be bothered. Now all you have to do is use something like the following code for it be up and running in seconds…
var t:Tweeter = new Tweeter();
t.proxy = "http://www.yourdomain.com";
t.user = "bbcbreaking";
t.getTweet();
addChild(t);
I’ve added a few other customisable options (which I will explain in a moment), but something like the above will have it working for you in seconds. Here’s a quick explanation of the above before I show you what else you can do with it…
t.proxy
As Zenbullets explains in his blog post, Flash and Twitter have cross-domain issues. So in order to bypass this you need to run a simple PHP file. I’ve included this file with the rest of the example files. All you have to do is upload it to your server and point t.proxy at the file’s location, as in the example above. Please ensure that you add “http://” at the beginning and you DON’T put a “/” at the end – Tweeter will do the rest. Please note: Tweeter will not work without this file.
t.user
This is simply the user name of the Twitter feed you want to pull. eg. if your Twitter page is “http://www.twitter.com/flashguru20″, the user name is “flashguru20″. If you forget to add this line it will default to The Burned Out Hippy’s Twitter feed.
t.getTweet()
When you’ve finished mucking around with the style (I’ll get to that in a minute), use this line to trigger Tweeter into action.
There are also a couple of other bits and bobs that you should know about that will allow you to style the tweet to your heart’s content. These are as follows (and should be added before the getTweet trigger):
t.picture = false
This will turn off the user’s profile picture. Currently the picture aligns to the left of the tweet. In the future I may adapt this so as to allow it’s location to be top, bottom, left and right – but as I don’t need to yet, I haven’t. So if the user pic isn’t fitting in with your styling of the text or simply isn’t to your liking, use the above line to turn it off.
t.loader
I imagine this is a bit naughty, but as I’m not fully AS3 conversant yet this is the only way I could achieve what I wanted to achieve. “loader” is quite simply the main URLRequest that calls the proxy PHP file to get the tweet. I’ve left it public in order for you to be able to add your own event listeners to it – so if you want to add a loading animation you can. I may close this back to private at a later date if my knowledge catches up and I find myself horrified at my own code (likely).
t.tweetField
This is the tweet’s textfield. Again, a bit naughty methinks, but it’s a means to an end. I’ve purposefully left it public in order for you to be able style the text to your heart’s content.
For example, to give the tweet a background colour and change it’s width you should do something like the following:
t.tweetField.background = true;
t.tweetField.backgroundColor = 0xeeeeee;
t.tweetField.width = 280;
You can also style the text field using a style sheet. The time and date are wrapped in a h1 tag and the main text is wrapped in a p. If you decide to use my CSSLoader class, all you would have to do is create a simple stylesheet and then type something like the following:
var cssLoader:CSSLoader = new CSSLoader(t.tweetField, t.tweetField.text, "http://www.yourdomain.com/tweetStyles.css");
I’ve included as much as I can in the example files so that you can get up and running as quickly as possible. Please make sure that you preserve the folder names of my classes or else nothing will work for you. Please also note that Tweeter uses my FindURL class, so don’t delete it by accident!
**************************************************************
You can download Tweeter and the other classes here
You can download the examples here
**************************************************************
I hope it’s useful to someone out there. I’ll be working on a multi-tweet version next (for multiple tweets from a single user) and possibly a keyword search version after that. If you’d like to see something particular changed or added to this version, please do let me know by leaving a comment below.
Cheerio!


Aleks says:
August 25th, 2009 at 8:56 am
thanks for this!
very useful. any news on the multi-tweet functionality?
cheers and thanks again.
The Hippy says:
August 31st, 2009 at 8:41 am
Glad it’s of use to you
I’ve just got back from my annual holidays so I’ll be working on the multi-tweet class over the next couple of weeks. I’m hoping to have it complete by mid-September so watch this space.
Marvin says:
September 6th, 2009 at 8:54 pm
hey there, awesome and simple class – i’ve changed some minor things to fade between different tweets just as a ticker. still needs some refinement in code but well, works for testing atm. gonna tell you when it’s in use
so far: thanks a lot for this great, great class.
ericapetrus says:
December 20th, 2009 at 9:42 pm
Thanks
Greg says:
January 6th, 2010 at 8:44 pm
Do you have a CS3 version?
Greg says:
January 6th, 2010 at 10:41 pm
Also, how can the @twitterId be added to the regExp in order to be clickable?
Gbolahan says:
January 25th, 2010 at 7:22 am
Hi,
Nice job! I have downloaded the Tweeter and the other classes and the examples but I cant open the .fla file, I keep getting “Unexpected file format” I’m using Adobe flash CS3 and actionScript 2.0.
Is there a way I can get a compatible file format to download?
Regards.
The Hippy says:
January 31st, 2010 at 4:54 pm
Hi guys. The files are not working for you because I am using Flash CS4. I’ve added a CS3 version of the FLA into the zip file – if you download it again, you should no longer have any issues.
Hope that helps!
Daan says:
February 6th, 2010 at 7:26 pm
Great job, i have been looking the web for this!
Any news on the multi-tweet functionality?
Thanks so far!
Daan
The Hippy says:
February 8th, 2010 at 9:55 am
Thanks Daan. I’m actually working on a multi-tweet class at the moment. I’m changing the methodology of it so it’s more adaptable. I can’t give you an exact timescale, but if you keep an eye on my twitter feed I’ll announce when it’s ready.
steve p says:
March 17th, 2010 at 1:14 pm
oh! i think this is just what i’ve been searching for! can’t wait to try it out! any luck with showing multiple tweets?
by the way i found this through your gotoAndLearn forum posting, so thank you for mentioning it there.
steve p says:
March 17th, 2010 at 3:02 pm
hi,
when i test this i get an error that reads:
TypeError: Error #1088: The markup in the document following the root element must be well-formed.
at com.plagro.tweets::Tweeter/SuccessFunc()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
the swf itself just says Loading…. Please wait 0%
I’m not quite sure what I’m missing, any suggestions?
The Hippy says:
March 17th, 2010 at 7:49 pm
Hi Steve. Sorry to hear that you’re having problems. It’s difficult to know what your error is without seeing how you’ve used my class in your code. I’ve just downloaded my classes though and they all seem to work just fine. A quick search for the error suggests that there is a problem with the XML (the tweets are read in as XML and held internally as XML) – but as the file is working here, I can only presume that there is a problem somewhere at your end with the file being read in. Here’s a quick checklist that might help:
1. Make sure that you have all of three classes installed – CSSLoader, FindURL and Tweeter and ensure that they are still following the correct ‘com.plagro’ paths.
2. Check to make sure that you AS3 com directory is set in the preferences panel within Flash CS4 (Edit > Preferences > Actionscript > Actionscript 3.0 Settings)
3. Make sure that you have uploaded the proxy file to somewhere on the same server that you are displaying the Tweet
4. Double check the path to the proxy file – make sure that you haven’t left a / on the end of the path.
If all of the above is correct then my example files should work just fine for you. Please let me know if your problem persists.
steve p says:
March 17th, 2010 at 11:29 pm
Mr. Hipster – Thanks for the response! I went back to your test files and managed to get it working. Looks like I was making a little mistake, but on top of that I was testing on a hosting site that doesn’t support PHP – bit of an issue eh?
Anyway its working now, thanks again for taking the time to share this with the online community!
Steve
The Hippy says:
March 21st, 2010 at 7:43 pm
Cheers Steve – I’m glad it’s working for you now. I’ll be working on the multi-feed class soon…so keep checking back!
Michel says:
May 13th, 2010 at 2:56 pm
Thanks for the tut!
Got it working with multiple tweets
Just some little adjustements in the Tweeter.as code:
First you’ll have to change the countnumber at the end of the url in the loadTweet-function to whatever number of tweets you’d like to display. I believe you can go to up to 200.
Secondly you’ll have to change this line of code in the SuccessFunc-function:
tweetField.htmlText=”"+nodes[y].substr(0,19)+”";
Because the function gets repeated for every tweet, you’re constantly overwriting the last tweet. Changing the “=” to “+=” (without the quotations of course) will add the older tweet instead of overwriting the previous (newer) tweet.
Finally, by adding the first tweet. The loading-text will stay displayed. You’ll have to remove this by resetting the tweetField.htmlText. You can do this by adding tweetField.htmlText=”" before calling the tweets.
Hope this helps!
Michel
The Hippy says:
May 15th, 2010 at 3:21 pm
Thanks Michel! Very useful!
As it happens I’m currently writing an entire library around Tweeter based on a new Tweeter Object. I’m working on creating something that will allow for single, multiple, searchable and more customisable tweets. It keeps getting put on hold due to other work, but if you keep an eye on my Twitter feed I will post when it is ready.
Thanks again!
Prashant Yashpal says:
May 26th, 2010 at 7:55 am
Hi,
I managed to get this working in my Flash Builder application, it worked fine offline testing, but as soon as i put it online it stopped working!
And now it does not work, either it shows “Loading please wait”
or it says “Sorry there seems to be an error with twitter”
I have uploaded proxy file and double checked all links
I still have no clue why this is happening ?
Any clues can anyone help?
Thanks!
The Hippy says:
May 26th, 2010 at 4:10 pm
Unfortunately I can’t help with individual cases, but the two things that I would suggest first are:
1. Try again. Sometimes the Twitter feed goes down for long periods of time. This is unfortunately out of my control and something that I experienced during the testing process myself. If it occurs, wait a few hours and try again.
2. Check your proxy. Make sure that you can contact the proxy directly and that its output is as expected.
I hope that helps in some way.
Prashant Yashpal says:
May 26th, 2010 at 7:47 pm
Hi
Thanks for the reply!
I fail to understand the problem, I have build the app using Flash builder, when I play from my desktop all loads super.
But as soon as I put it online the same files , it does not load.
Does the proxy file and the app directory online have to be the same?
Or is it enough that the proxy is on the same server?
for eg. in my app the proxy is at . http://www.xyz.com/twitterar
(the proxy file is sitting here)
when i deploy my app it is at . http://www.xyz.com/mobar
this works perfect on my desktop but nothing happens online
Thanks for your help and a great app.
Hope i can figure this out and fix it.
Thanks
Prashant Yashpal says:
May 26th, 2010 at 8:09 pm
Hi
sorry to post so many times, just one question
do i need a crossdomain.xml file?
Thanks a lot!
Prashant Yashpal says:
May 26th, 2010 at 8:25 pm
Hi,
Okay for the last time, I got my app to work its showing my the twitter feed online too! yay! thanks.
The problem was coming from the fact, that i wanted the twitter profile picture to be loaded, but there is a sandbox problem with that!.
I will see if i find a solution to that.
Basically I am trying to use this to augmented a twitter feed. using flartoolkit.
if you like I can post a link here for the existing working app, but with no profile picture.
Thanks again for your super class. and help!
The Hippy says:
June 13th, 2010 at 2:15 pm
Hi again Prashant. I’m glad that you finally got it working – please do post a link here, I’d love to see it in action.
Many thanks!
TechFlashGuy says:
June 18th, 2010 at 8:35 am
I love this thing. Some great info here.
I need however, to load this into a as2 swf (I know that this is not possible), but is there any way to convert this code to as2?
Thanks.
The Hippy says:
June 18th, 2010 at 9:24 am
The class isn’t very complicated, so I’d imagine that if you’re ok with AS2 OOP you should be able to do a straightforward conversion. Although you’d probably need to change the other classes as well. Personally however, I’m hard at work on developing a load of new AS3 twitter classes so I don’t currently have any plans to release an AS2 version. Possibly in the future though, if there’s enough demand.
In the meantime, if you’re forced to use AS2, the only help I can offer is this thread: http://tinyurl.com/26xhvdf
Thanks for your comments!