Official website for Web Designer - defining the internet through beautiful design
FOLLOW US ON:
Author: Mark Billen
4th February 2010

Flash Week Day #4 – Twitter feed

10 Assigning the tweets

10 Assigning the tweets

Flash Week Day #4 – Twitter feed

Create a new function called detailsResultHandler passing in an Object as a parameter. The object holds the returned Twitter information. You get to the information the same way you would do with an array, so object.result[0].text would be the first tweet. Assign all of the five dynamic text fields their results.

function detailsResultHandler(e:Object):void {

one.text = e.result[0].text
two.text = e.result[1].text;
three.text = e.result[2].text;
four.text = e.result[3].text;
five.text = e.result[4].text;

11 Format the date

Flash Week Day #4 – Twitter feed

Because we wanted the date or time posted to look the same as it does on Twitter you have to do a few calculations to turn the long date into something like ‘Posted 3 hours ago’. Start by adding a loop so you can do all of the five text fields in the same code block, then copy the code below to get days, hours and minutes.

sort dates
for (var i=0; i<5; i++) {
var currentDate:Date = new Date();
var postDate:Date=new Date(e.result[i].created_at);
var diffDate:Number=new Number(currentDate.getTime()-postDate.getTime());

var day:Number=1000*60*60*24;
var days:Number=Math.floor(diffDate/day);
var rem:Number=diffDate%day;
var hour:Number=day/24;
var hours:Number=Math.floor(rem/hour);
var rem2:Number=diffDate%hour;

var min:Number=hour/60;
var mins:Number=Math.floor(rem2/min);

12 Create the date string
Now you have your calculated day, hour and minutes let’s use an if statement to check whether the day, hour and minutes are not equal to 0. If they are not then amend the string. Lastly hide the hopping bird loader.

build string
var posted:String=”Posted: “;
if (days!=0) {
posted+=String(days)+” days, “;
}
if (hours!=0) {
posted+=hours+” hours, “;
}
if (mins!=0) {
posted+=mins+” mins “;
}
posted += “ ago.”;
this[“created”+i].text=String(posted);

}

loader.visible=false;

}

13 Exception handling
Add three functions which will deal with your error handling; there is nothing worse than running a program and not having a clue if the data is being returned and so on. These functions will give you feedback if an error triggers. Close the class and packages with curly braces and save.

function progressHandler(e:Object):void {
trace(“Loading: “ + e.bytesLoaded + “ of “ + e.bytesTotal);
}
function timeoutHandler(e:Object):void {
trace(“ERROR: operation timed out”);
}
function faultHandler(e:Object):void {
trace(“ERROR: “ + e.fault.message);
}
} // close class
}// close package

14 Final touches
Open up the fla file again and run your movie, you should first see the hopping blue bird for a few seconds then your last five Twitter posts will appear. All that’s left to do is publish your files which can be done in the Properties Inspector. Once you have done that you can copy the HTML object code and place it in your blog – not bad eh?

(This article originally appeared in Web Designer 157, authored by Darren Richardson)

Pages: 1 2 3

  • Tell a Friend
  • Follow our Twitter to find out about all the latest web development, news, reviews, previews, interviews, features and a whole more.

    8 Comments »

    • Matt Rouse said:

      This does not function.

      Like the Paralax scrolling code from the same magazine, which I had to spend an hour debugging to get it to run properly. Is this magazine designed to frustrate me? I want these projects, but the sample code doesn’t run.

      Error #2044: Unhandled StatusEvent:. level=error, code=

    • Matt Rouse said:

      Oh, and the data in the Output panel is coming back correct up to that point, except the URL which is showing as ‘undefined’.

      The posts are displayed with content like:

      “reess has desed a e este r ahse
      tds ret assaes ated at the eter”
      Date: sted hrs a

      Help!

    • Adam said:

      Nice tutorial,

      It was my first attempt at using .as scripts in my work and you made it pretty straight forward.

      Quick question, does using this SWX stuff get around the 150 maximum connetions to twitter each hour problem?

      Cheers

      Adam

    • Matt Rouse said:

      I finally fixed it.

      For future reference, in CS5, you need to turn off debug mode to fix the error and you need to embed a font and then select each text area and select the font you have embedded.

    • Jopasety said:

      ive tried pretty much everything dude.. :/ and i dont get it!
      what im doing wrong!? i follow ur last steps and it didnt work for me! :( how do u disable the debug mode!? thx in advance!

    • Ian said:

      It appears the free swx.php portal is now having some problems.

      TypeError: Error #1010: A term is undefined and has no properties.
      at TwitterSWX/detailsResultHandler()
      at flash.events::EventDispatcher/dispatchEventFunction()
      at flash.events::EventDispatcher/dispatchEvent()
      at org.swxformat::ExternalAsset/onMovieReady()

      is now being displayed when it wasn’t before. Great tutorial, and the end result lasted me for a good week or so since I bought the web designer mag volume 5. I’m going to try and crack this but any help would be appreciated.

    • Joanna said:

      All I’m seeing is the hopping bird, no Twitter posts. What am I doing wrong? I’m using CS5

    • Joanna said:

      I’m just going to link it in with this http://twitter.com/goodies/widgets

    What's your opinion?

    Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

    Be nice. Keep it clean. Stay on topic. No spam.

    * Required fields