Tag Archives: as3

Rotation Property Weirdness

Actionscript:
  1. var boxA:Shape = Shape(addChild(new Shape()));
  2. with (boxA.graphics) beginFill(0), drawRect(-10,-10,20,20);
  3.  
  4. var boxB:Shape = Shape(addChild(new Shape()));
  5. with (boxB.graphics) beginFill(0), drawRect(-10,-10,20,20);
  6.  
  7. boxA.x = 100;
  8. boxA.y = 100;
  9.  
  10. boxB.x = 200;
  11. boxB.y = 100;
  12.  
  13. var rot:Number = 32750;
  14.  
  15. addEventListener(Event.ENTER_FRAME, onLoop);
  16. function onLoop(evt:Event):void {
  17.   rot += 1
  18.   // will stop rotating
  19.   boxA.rotation = rot
  20.   // will keep rotating
  21.   boxB.rotation = rot % 360;
  22. }

I recently became aware of a strange aspect of the rotation property on DisplayObjects. For some reason, once it's value goes a little beyond ~32750 the DisplayObject will simply stop rotating. If you read the rotation property it is still changing, but there is no visual update - a quick check on the DisplayObject.transform.matrix property will show that the value has stopped.

The easy fix is to use mod before applying the value to the rotation property. Surprised I've never come across this one before. Maybe someone can shed some light on this.


// for people searching google for solutions to this problem I'll add the following key words:
MovieClip stops rotating, DisplayObject stops rotating, rotation property broken, not rotating

Posted in DisplayObject, misc, motion | Also tagged , | 1 Comment

Four by Four

Two years ago I had an idea for a programming experiment. After writing a proof of concept in Processing, I never did anything else with it. A few weeks back I decided to revisit the project and I created a finalized version. Have a look at it here (the instructions are important so be sure to read them):

Posted in Uncategorized | Also tagged , , | 4 Comments

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 | Also 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 | Also tagged , | 15 Comments