Monthly Archives: April 2009

Plot of sinh()

Actionscript:
  1. x = stage.stageWidth / 2;
  2. y = stage.stageHeight / 2;
  3. scaleX = scaleY = 3;
  4.  
  5. var t:Number =-5;
  6. var xp:Number = 0;
  7. var yp:Number = 0;
  8.  
  9. addEventListener(Event.ENTER_FRAME, onLoop);
  10. function onLoop(evt:Event):void {
  11.      xp = t;
  12.      yp = sinh(t);
  13.      
  14.     graphics.lineStyle(0,0);
  15.     if (t == -5){
  16.         graphics.moveTo(xp, yp);
  17.     }else{
  18.         graphics.lineTo(xp, yp);
  19.     }
  20.      if (t> 5){
  21.          removeEventListener(Event.ENTER_FRAME, onLoop);
  22.      }
  23.      t+=.2;
  24. }
  25. function sinh(x:Number):Number{
  26.     return (Math.pow(Math.E, x) - Math.pow(Math.E, -x)) * 0.5;
  27. }

Just a quick plot to test the sinh() function from a few days ago...

Posted in Math | Tagged , | Leave a comment

Relative Positioning

Actionscript:
  1. var size:Array = [1, 1.5, .5, 1, .4, 1, 1, 1, .2, 1.1]
  2. var boxes:Array = new Array();
  3. var spacing:Number = 4;
  4. var container:Sprite = Sprite(addChild(new Sprite()));
  5. container.x = container.y = 100;
  6.  
  7. for (var i:int = 0; i<size.length; i++){
  8.     var box:Sprite = makeBox();
  9.     var prev:int = i - 1;
  10.     box.scaleX= box.scaleY = size[i];
  11.     if (i == 0){
  12.         box.y = 10;
  13.     }else{
  14.         // here's the trick
  15.         // if you animate the height property you need to do this again and again:
  16.         box.y = boxes[prev].y + boxes[prev].height/2+ box.height/2 + spacing
  17.     }
  18.     boxes.push(box);
  19. }
  20.  
  21. function makeBox():Sprite{
  22.     var box:Sprite = Sprite(container.addChild(new Sprite()));
  23.     with (box.graphics) beginFill(0xFF0000), drawRect(-50,-10, 100, 20);
  24.     return box;
  25. }

Sometimes you need to position a bunch of Sprites or MovieClips that are different sizes - and you want to keep the spacing between them the same. This snippet shows a simple example of this.

For more info you could also do this tutorial that I wrote on learningactionscript3.com

Posted in UI, misc | Tagged , | Leave a comment

Shapevent Log (not a snippet)

Recently launched a new project over at shapevent.com. If your enjoy sketchbooks, drawings and interactive ugliness... have a look here:

Shapevent Log

Many of the techniques covered in the code on this website will be used in shapevent log entries... no rss feed yet, but I'll have one up for it in the next few days....

Posted in misc | Leave a comment

sinh & cosh

Actionscript:
  1. function sinh(x:Number):Number{
  2.     return (Math.pow(Math.E, x) - Math.pow(Math.E, -x)) * 0.5;
  3. }
  4.  
  5. function cosh(x:Number):Number{
  6.     return (Math.pow(Math.E, x) + Math.pow(Math.E, -x)) * 0.5;
  7. }

Needed sinh and cosh today. Easy enough to create with existing math functions. If you needed more speed you could inline these and replace Math.E with 2.71828183.

Got the math over at wikipedia (as usual).

Posted in Math, misc | Tagged , | Leave a comment