Category Archives: external data

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...

Also posted in string manipulation, strings | Tagged , , , | 5 Comments

I’m Twitter Search

Actionscript:
  1. [SWF(width = 400, height=600, frameRate=12)]
  2. var loader:URLLoader = new URLLoader();
  3. var req:URLRequest = new URLRequest("http://search.twitter.com/search.atom");
  4. var vars:URLVariables = new URLVariables();
  5. vars.q = "I'm";
  6. // results per page
  7. vars.rpp = "7";
  8. vars.page = 1;
  9.  
  10. req.data = vars;
  11. req.method = URLRequestMethod.GET;
  12.  
  13. loader.addEventListener(Event.COMPLETE, onLoaded);
  14. loader.load(req);
  15.  
  16. // check for updates every 30 seconds -
  17. var timer:Timer = new Timer(30000);
  18. timer.addEventListener(TimerEvent.TIMER, onTick);
  19. timer.start();
  20. function onTick(evt:TimerEvent):void{
  21.     txt.htmlText = "loading...";
  22.     loader.load(req);
  23. }
  24.  
  25. var txt:TextField = TextField(addChild(new TextField()));
  26. txt.defaultTextFormat = new TextFormat("_sans", 12);
  27. with (txt){ x=10, y=10, width=stage.stageWidth-10;
  28. height=stage.stageHeight, multiline = true, wordWrap = true; }
  29. txt.htmlText = "loading...";
  30.                                                    
  31. function onLoaded(evt:Event):void{
  32.     var searchData:XML = new XML(loader.data);
  33.     var atom:Namespace = searchData.namespace("");
  34.     default xml namespace = atom;
  35.     var htmlText:String = "<b>Last " + vars.rpp + " '" + vars.q + "' tweets:</b><br><br>";
  36.     for each(var entry:XML in searchData.entry){
  37.         htmlText += entry.author.name.toString() + ": <br>";
  38.         htmlText += entry.title.toString() + "<br><br>";
  39.     }
  40.     txt.htmlText = htmlText;
  41. }

Was curious about the twitter search api so spent a few minutes and whipped up a quick demo - the api is simple and easy to understand...

As a test I created something that shows the last 7 tweets including the phrase I'm. A new search is done every 30 seconds... I chose the phrase I'm so that there will be different results every time a new search is done...

Read More »

Also posted in XML | Tagged , , , | 1 Comment

Cut Image Into Squares

Actionscript:
  1. [SWF(width=650, height=650)]
  2. var loader:Loader = new Loader();
  3. loader.load(new URLRequest("http://actionsnippet.com/wp-content/chair.jpg"));
  4. loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
  5. x = y = 25;
  6. var w:Number;
  7. var h:Number;
  8. var rows:Number = 20;
  9. var cols:Number = 20;
  10. var tiles:Vector.<Sprite> = new Vector.<Sprite>();
  11. function onLoaded(evt:Event):void{
  12.     w = evt.target.width;
  13.     h = evt.target.height;
  14.     var image:BitmapData = Bitmap(evt.target.content).bitmapData;
  15.     var tileWidth:Number = w / cols;
  16.     var tileHeight:Number = h / rows;
  17.     var inc:int = 0;
  18.     var pnt:Point = new Point();
  19.     var rect:Rectangle = new Rectangle(0,0,tileWidth,tileHeight);
  20.     for (var i:int = 0; i<rows; i++){
  21.         for (var j:int = 0; j<cols; j ++){
  22.              var currTile:BitmapData= new BitmapData(tileWidth, tileHeight, true, 0x00000000);
  23.              rect.x = j * tileWidth;
  24.              rect.y = i * tileHeight;
  25.              currTile.copyPixels(image, rect, pnt, null, null, true);
  26.              var bit:Bitmap = new Bitmap(currTile, "auto", false);
  27.              var s:Sprite = tiles[inc] = Sprite(addChild(new Sprite()));
  28.              // offset them a little bit to show that they are in fact tiles.
  29.              s.x = rect.x + Math.random()*10 - 5;
  30.              s.y = rect.y + Math.random()*10 - 5;
  31.              
  32.             /* If you have TweenLite, try something like this:
  33.              s.x = rect.x;
  34.              s.y = rect.y;
  35.              TweenLite.from(s, 0.3, {x:Math.random()*stage.stageWidth, y:Math.random()*stage.stageHeight, alpha:0, delay:inc / 50});
  36.              */
  37.              s.addChild(bit);
  38.              inc++;
  39.         }
  40.     }
  41. }

This snippet shows how to cut a dynamically loaded image up into small squares (or rectangles). Each square is placed into a sprite and these sprites are stored in a vector for later use. This kind of thing could be useful in conjunction with tweening engines to create transition effects. If you have TweenLite on your machine you could add the import statement "import gs.TweenLIte" and uncomment lines 33-35.

For the purposes of this demo I just offset each sprite slightly to show that it the loaded image has in face been cut up. Here are a few stills with created by altering the rows and cols variables:

Without any animation, this snippet is a bit boring - again, using TweenLite or some other tweening engine is the way to go.

Also posted in BitmapData | Tagged , , , | 12 Comments

FileReference.load()

Actionscript:
  1. var txt:TextField = TextField(addChild(new TextField()));
  2. txt.autoSize = TextFieldAutoSize.LEFT;
  3. txt.x = txt.y = 20;
  4. txt.text = "click anywhere to load an image file...";
  5.  
  6. var fileRef:FileReference= new FileReference();
  7. stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
  8. function onDown(evt:MouseEvent):void{
  9.     fileRef.browse([new FileFilter("Images", "*.jpg;*.gif;*.png")]);
  10.     fileRef.addEventListener(Event.SELECT, onSelected);
  11.     stage.removeEventListener(MouseEvent.MOUSE_DOWN, onDown);
  12. }
  13. function onSelected(evt:Event):void{
  14.     fileRef.addEventListener(Event.COMPLETE, onLoaded);
  15.     fileRef.load();
  16.     fileRef.removeEventListener(Event.SELECT, onSelected);
  17. }
  18. function onLoaded(evt:Event):void{
  19.     var loader:Loader = new Loader();
  20.     loader.loadBytes(evt.target.data);
  21.     addChild(loader);
  22.     fileRef.removeEventListener(Event.COMPLETE, onLoaded);
  23. }

This snippet shows how to use the FileReference.load() method to load an image into flash player RAM and display it on the stage.

Also posted in misc | Tagged , , | Leave a comment