Monthly Archives: May 2009

More Messy Code

Actionscript:
  1. var size:Number = 800;
  2. var canvas:BitmapData = new BitmapData(size,size,false, 0x000000);
  3. addChild(new Bitmap(canvas, "auto", true));
  4.  
  5. scaleX = scaleY = .5;
  6. var pix:Number = size * size;
  7. var scale:Number = 1/(size/3);
  8.  
  9. for (var i:Number = 0; i<pix; i++){
  10.        var xp:Number = (i % size);
  11.        var yp:Number = int(i / size);
  12.        var xt:Number = xp * scale;
  13.        var yt:Number = yp * scale;
  14.        var ca:Number =  (Math.abs(Math.tan(yt) * Math.pow(Math.sin(xt),3)) * 100 ) % 155;
  15.        var cb:Number =  (Math.abs(Math.tan(xt) * Math.pow(Math.sin(yt),3)) * 100)  % 155;
  16.        ca|= cb;
  17.        canvas.setPixel(xp, yp,  ca <<16 | ca <<8 | ca);
  18. }

Another messy code snippet that I e-mailed to myself at some point...

Try replacing line 16 with some of these variations:

ca &= cb;
ca += cb;
ca -= cb;
ca ^= cb;
ca %= cb

Posted in BitmapData, misc, pixel manipulation, setPixel | Tagged , | Leave a comment

QuickBox2D Documentation

I finally finished the QuickBox2D documentation. Check it out here.

You can download QuickBox2D and see a few example code snippets on the QuickBox2D page. Should have a tutorial on that page in the next couple of days.

Posted in Box2D, QuickBox2D, motion | Tagged , | 5 Comments

Instantiate and Set Properties

Actionscript:
  1. function create(obj:Class, props:Object):*{
  2.     var o:* = new obj();
  3.     for (var p:String in props){
  4.         o[p] = props[p];
  5.     }
  6.     return o;
  7. }
  8.  
  9. // test out the function
  10.  
  11. var txt:TextField = create(TextField, {x:200, y:100, selectable:false, text:"hello there", textColor:0xFF0000, defaultTextFormat:new TextFormat("_sans", 20)});
  12. addChild(txt);
  13.  
  14. var s:Sprite = Sprite(addChild(create(Sprite, {x:100, y:100, rotation:45, alpha:.5})));
  15.  
  16. with (s.graphics) beginFill(0xFF0000), drawRect(-20,-20,40,40);
  17.  
  18. var blur:BlurFilter = create(BlurFilter, {blurX:2, blurY:8, quality:1});
  19.  
  20. s.filters = [blur];

This snippet shows a function called create() that takes two arguments. The first argument is the name of a class to instantiate. The second is an Object with a list of properties to set on a newly created instance of the class (referenced in the first argument).

This could be particularly useful for TextFields which for some reason have no arguments in their constructor.

This will currently only work for classes that have either all optional constructor arguments or no constructor arguments.

Posted in dynamic, functions, one-liners, properties | Tagged , | Comments closed

Messy Code

Actionscript:
  1. var xp:Number=0,yp:Number=0;
  2. var r:Number=0,t:Number=0;
  3. var speed:Number=.01;
  4. var scale:Number=10;
  5. var shape:Shape = Shape(addChild(new Shape()));
  6. shape.x=stage.stageWidth/2;
  7. shape.y=stage.stageHeight/2;
  8. shape.graphics.lineStyle(0,0x000000);
  9.  
  10. var s:Number=100;
  11. var range:Number=.2;
  12. var b:Number=20;
  13. var a:Number =b*range;
  14.  
  15. addEventListener(Event.ENTER_FRAME, onLoop);
  16. function onLoop(evt:Event):void {
  17.     range=.2;
  18.     b=20;
  19.     a=b*range;
  20.     t=0;
  21.     shape.graphics.clear();
  22.     shape.graphics.lineStyle(0,0);
  23.     for (var i:int = 0; i<6000; i++) {
  24.         var sin:Number=Math.cos(t*int(mouseY/20));
  25.         r =  Math.sqrt(40 * b * (b - a * (Math.pow(sin, int(mouseX/20)))));
  26.         xp=r*Math.cos(t);
  27.         yp=r*Math.sin(t);
  28.  
  29.         if (t==0) {
  30.             shape.graphics.lineStyle(0, 0);
  31.             shape.graphics.moveTo(xp, yp);
  32.         } else {
  33.             shape.graphics.lineTo(xp, yp);
  34.         }
  35.         t+=speed;
  36.         if (t> (2 * Math.PI + speed)) {
  37.             range+=.3;
  38.             a=b*range;
  39.             t=0;
  40.         }
  41.     }
  42. }

This is an example of raw messy code that draws some interesting shapes. I was waiting somewhere with my laptop a few weeks back and I wrote this and e-mailed this to myself - it's completely unoptimized and unedited.

I was working on documentation for QuickBox2D today but didn't finish it - so I posted this instead. I hope to finish the docs for tomorrow...

Posted in misc | Tagged , | Leave a comment