Monthly Archives: December 2009

Graphics Class and Twitter

Actionscript:
  1. [SWF(width = 700, height=700, frameRate=12)]
  2. var cmds:Array = [];
  3.  
  4. var loader:URLLoader = new URLLoader();
  5. var req:URLRequest = new URLRequest("http://search.twitter.com/search.atom");
  6. var vars:URLVariables = new URLVariables();
  7. vars.q = "#asgraph";
  8. // results per page
  9. vars.rpp = "100";
  10. vars.page = 1;
  11.  
  12. req.data = vars;
  13. req.method = URLRequestMethod.GET;
  14.  
  15. loader.addEventListener(Event.COMPLETE, onLoaded);
  16. loader.load(req);
  17.  
  18. var txt:TextField = TextField(addChild(new TextField()));
  19. txt.defaultTextFormat = new TextFormat("_sans", 12);
  20. with (txt){ x=10, y=10, autoSize="left"; }
  21. txt.htmlText = "loading...";
  22.  
  23. function onLoaded(evt:Event):void{
  24.        removeChild(txt);
  25.    var searchData:XML = new XML(loader.data);
  26.    var atom:Namespace = searchData.namespace("");
  27.  
  28.    var leng:int = searchData.atom::entry.length() -1;
  29.   for (var i:int = leng; i>=0; i--){
  30.        var cmd:String =
  31. searchData.atom::entry[i].atom::title.toString().replace(/\#asgraph/g,
  32. "");
  33.       // added this to ignore words starting with @
  34.       cmd = cmd.replace(/@(\w+)/g, "");
  35.        cmds.push(cmd);
  36.    }
  37.  
  38.        var time:Timer = new Timer(100, cmds.length);
  39.        time.addEventListener(TimerEvent.TIMER, onTick);
  40.    time.start();
  41. }
  42. function onTick(evt:TimerEvent):void{
  43.        render(parseFunctions(cmds[evt.target.currentCount - 1]));
  44.        graphics.endFill();
  45.        graphics.lineStyle();
  46. }
  47.  
  48. // parse and run Graphics class commands
  49. function parseFunctions(dat:String):Array{
  50.    var a:Array = dat.split(";") ;
  51.    for (var i:int = 0; i<a.length-1; i++){
  52.        a[i] = a[i].split(/\(\)|\(|\)/g);
  53.        var f:String = a[i][0] = a[i][0].replace(/\s/g,"");
  54.        a[i] = a[i].splice(0, a[i].length - 1);
  55.        if (a[i].length> 1){
  56.         a[i] = a[i][1].split(",");
  57.         a[i].unshift(f);
  58.        }
  59.    }
  60.    return a.splice(0,a.length - 1);
  61. }
  62. function render(p:Array):void {
  63.    for (var i:int = 0; i<p.length; i++) {
  64.        try{
  65.        graphics[p[i][0]].apply(graphics,p[i].splice(1));
  66.        }catch(e:Error){};
  67.    }
  68. }

This is a simple idea I had awhile back... This snippet searches twitter for the #asgraph hashtag and if it finds standard Graphics class method calls it renders them.

So if you tweet something likethis :

#asgraph beginFill(0xFF); drawCircle(200,200,10);

it will get rendered into the below swf (you'll need to refresh to see your tweet get rendered):

This movie requires Flash Player 9

Here is a direct link to the swf

Posted in Graphics, external data | Tagged , , | 3 Comments

Paste an Arbitrary Piece of Code

Actionscript:
  1. ...
  2. // loop through until we find the root note
  3.     // grab the third and the fifth and exit the loop
  4.     for (var i:int = 0; i<leng; i++){
  5.         if (cMajor[i] == note){
  6.             third = cMajor[(i + 2) % leng];
  7.             fifth = cMajor[(i + 4) % leng];
  8.             break;
  9.         }
  10.     }
  11.    
  12.     // we may need a double sharp on the middle note
  13.     var sharpFlatDouble:String = sharpFlat;
  14.    
  15.     // check if this is a sharp, check if it is A or D
  16.     // if it is add the symbol for double sharp
  17.     if (sharpFlat == "#"){
  18.         if (note == "D" || note == "A"){
  19.             sharpFlatDouble = "x";
  20.         }
  21.     }
  22. ...

If your working on some code... just randomly copy a piece of it and paste it in the comments... This code is from a program that generates any major scale (it's still not finished). Feel free to post code chunks in any language...

[EDIT] the code doesn't need to work on its own... you can just randomly copy from something your working on...

Posted in misc | Tagged , , | 15 Comments

QuickBox2D Mini-Poly Editor

Actionscript:
  1. [SWF(width = 800, height = 600, frameRate = 60)]
  2. import com.actionsnippet.qbox.*;
  3. stage.frameRate = 60;
  4.  
  5. var sim:QuickBox2D = new QuickBox2D(this);
  6.  
  7. sim.createStageWalls();
  8.  
  9. sim.start();
  10.  
  11. var output:TextField = new TextField();
  12. output.text = "Click anywhere to add points to a polygon. Hit any key to test.\n\n";
  13. output.x = output.y = 50;
  14. with(output) width = 300, height = 400, border = true, selectable = true, wordWrap = true, multiline = true;
  15. addChild(output);
  16.  
  17. function display(str:*):void{
  18.     output.appendText(str.toString() + "\n");
  19. }
  20.                                
  21. var points:Array = [];
  22. var poly:Shape = new Shape();
  23. addChild(poly);
  24.  
  25. stage.addEventListener(MouseEvent.CLICK, onClick);
  26. stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
  27.  
  28. function onClick(evt:MouseEvent):void {
  29.     if (points.length == 0){
  30.         poly.graphics.beginFill(0xCCCCCC);
  31.         poly.graphics.lineStyle(1, 0xFF0000);
  32.         poly.graphics.moveTo(mouseX, mouseY);
  33.     }else{
  34.         poly.graphics.lineTo(mouseX, mouseY);
  35.     }
  36.     poly.graphics.drawCircle(mouseX, mouseY, 2);
  37.      
  38.     points.push(mouseX / 30.0, mouseY / 30.0);
  39. }
  40.  
  41. function onKeyPressed(evt:KeyboardEvent):void {
  42.      // average all points
  43.      var avgX:Number=0
  44.      var avgY:Number = 0;
  45.      
  46.      for (var i:int = 0; i<points.length; i+=2){
  47.          avgX += points[i];
  48.          avgY += points[i + 1];
  49.      }
  50.    
  51.      avgX /= points.length/2;
  52.      avgY /=  points.length/2;
  53.      avgX = avgX;
  54.      avgY = avgY;
  55.      
  56.      // subtract averages and fix decimal place
  57.       for (i = 0; i<points.length; i+=2){
  58.           var yp:int = i + 1;
  59.           points[i] -= avgX;
  60.           points[yp] -= avgY;
  61.           points[i] = Number(points[i].toFixed(2));
  62.           points[yp] = Number(points[yp].toFixed(2));
  63.      }
  64.      
  65.      display("points array:");
  66.      display(points);
  67.      
  68.      try{
  69.          var p:QuickObject = sim.addPoly({x:avgX, y:avgY, points:points});
  70.          p.userData.graphics.beginFill(0xFF0000);
  71.          p.userData.graphics.drawCircle(0,0,5);
  72.      }catch(e:*){
  73.         display("Invalid polygon data!");
  74.      }
  75.      
  76.      poly.graphics.clear();
  77.      points = [];
  78. }

This snippet shows the basic concepts needed to go about creating a polygon editor for QuickBox2D. I have an unreleased editor that I use for my QuickBox2D projects, at some point I may release it... but for now I figured I'd post this extremely simplified version for people to expand on.


Have a look at the swf here...

Posted in Box2D, QuickBox2D | Tagged , , , , | 26 Comments