I want Twitter

Actionscript:
  1. [SWF(width = 800, height=600, frameRate=15)]
  2.  
  3. var littleTxt:TextField = TextField(addChild(new TextField()));
  4. with (littleTxt){
  5.     defaultTextFormat = new TextFormat("_sans", 10);
  6.     width = stage.stageWidth-20;
  7.     height = stage.stageHeight-20;
  8.     multiline = true;
  9.     wordWrap = true;
  10.     text ="";
  11.     textColor = 0x444444;
  12.     selectable = false;
  13.     x = y = 10;
  14. }
  15.  
  16. var txt:TextField = TextField(addChild(new TextField()));
  17. with (txt){
  18.     defaultTextFormat = new TextFormat("_sans", 90);
  19.     autoSize = "center";
  20.     x = 0
  21.     y = stage.stageHeight / 2 - 61
  22.     width = stage.stageWidth;
  23.     selectable = false;
  24.     text = "loading";
  25. }
  26.  
  27. var selectWords:Array = [];
  28. addEventListener(Event.ENTER_FRAME, onLoop);
  29. function onLoop(evt:Event):void {
  30.      if (selectWords.length> 0){
  31.         var word:String = selectWords[int(Math.random() * selectWords.length)];
  32.         txt.text = word;
  33.         littleTxt.appendText(word+" ");
  34.      }
  35. }
  36.  
  37. var loader:URLLoader = new URLLoader();
  38. var req:URLRequest = new URLRequest("http://search.twitter.com/search.atom");
  39. var vars:URLVariables = new URLVariables();
  40. vars.q = "i want";
  41. // results per page
  42. vars.rpp = "50";
  43. vars.page = 1;
  44. vars.lang = "en";
  45.  
  46. req.data = vars;
  47. req.method = URLRequestMethod.POST;
  48. loader.load(req);
  49. loader.addEventListener(Event.COMPLETE, onLoaded);                                 
  50. function onLoaded(evt:Event):void{
  51.     var searchData:XML = new XML(loader.data);
  52.     var atom:Namespace = searchData.namespace("");
  53.     default xml namespace = atom;
  54.     var titles:String = "";
  55.     for each(var entry:XML in searchData.entry){
  56.         titles += entry.title.toString();
  57.     }
  58.     var index:int = 0;
  59.     var words:Array = titles.split(" ");
  60.     for (var i:int = 0; i<words.length; i++){
  61.         // exclude a few things
  62.         if (words[i].match(/RT|\@|\#|http/g).length == 0){
  63.             selectWords[index++] = words[i];
  64.         }
  65.     }
  66. }

This snippet searches twitter for the phrase "I want" and then displays the results randomly word by word in a somewhat hypnotic way.


Have a look at the swf...

This entry was posted in external data, string manipulation, strings and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

5 Comments

  1. Vipin
    Posted August 3, 2009 at 12:32 pm | Permalink

    This doesnt work properly (the layout) when I do it from a flex builder as3 project.

  2. Posted August 3, 2009 at 12:45 pm | Permalink

    try tracing out your stage.stageWidth and stage.stageHeight values… you may need to add a delay to make sure they are properly populated…. I’ll check it in flex real quick to see if it happens to me…

  3. Posted August 3, 2009 at 1:08 pm | Permalink

    just tested it in flex… works fine with a fixed width/height swf… just don’t forget to add this:

    [SWF(width = 800, height=600, frameRate=15)]

    and if you want to scale the swf use those hardcoded values 800, 600 rather than reading the stage width and height… if you want to scale the swf and not the text you’ll need to do something a tad different…

  4. Brendan Lee
    Posted August 3, 2009 at 1:38 pm | Permalink

    you should get on twitter. Even if you have an automated tweet that happens when you update, there are a lot of people that would really regular updates on your work here ( myself included )

    - best

    Brendan

  5. Posted August 3, 2009 at 1:44 pm | Permalink

    hmmm the automated twitter thing is a good idea… since people have tweetdeck and stuff running… I may do that in the next day or two. Thanks for the idea Brendan.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*