Tag Archives: actionscript

Drawing Gears

Actionscript:
  1. const TWO_PI:Number=Math.PI*2;
  2. // x, y, max radius, notch number
  3. drawVerts(calcGear(200, 200, 50, 9));
  4.  
  5. drawVerts(calcGear(400, 200, 30, 3));
  6.  
  7. drawVerts(calcGear(300, 350, 30, 5));
  8.  
  9. drawVerts(calcGear(400,400, 30, 2));
  10.  
  11. function calcGear(x:Number, y:Number, maxRad:Number, s:int):Array {
  12.     var verts:Array = new Array();
  13.     var step:Number=TWO_PI / (s * 4);
  14.     var mod:Number=0;
  15.     for (var i:Number = 0; i<=TWO_PI; i+=step) {
  16.         var r:Number = (int(mod)%2+1) * maxRad;
  17.         mod+=.5;
  18.         verts.push(x + r * Math.cos(i));
  19.         verts.push(y + r * Math.sin(i));
  20.     }
  21.     return verts;
  22. }
  23.  
  24. // could  use draw path here instead;
  25. function drawVerts(verts:Array):void{
  26.     graphics.lineStyle(0,0x000000);
  27.     graphics.moveTo(verts[0], verts[1]);
  28.     for (var i:int = 2; i<verts.length; i+=2) {
  29.         graphics.lineTo(verts[i], verts[i + 1]);
  30.     }
  31.     graphics.lineTo(verts[0], verts[1]);
  32. }

Needed to draw some gear shapes today...

Posted in Graphics | Also tagged | Leave a comment

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 | Also 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 | Also tagged | 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 | Also tagged | Leave a comment