Tag Archives: actionscript

const

Actionscript:
  1. const HALF_PI:Number = Math.PI / 2;
  2. const RADS:Number = Math.PI / 180;
  3.  
  4. trace(HALF_PI);
  5. // convert degrees to radians
  6. trace(180 * RADS);

When I have constant values I often forget to go ahead and make use of the const keyword. I used to do this a good deal when AS3 first came out.... not sure why I stopped doing it. Anyway, hopefully this post will help remind me to use them...

A very common use for them is of course defining event types:

Actionscript:
  1. // inside a class
  2. public static const RENDER_COMPLETE:String = "renderComplete";

Posted in variables | Also tagged | Leave a comment

Sine Cosine Walk

Actionscript:
  1. stage.frameRate = 30;
  2. var footA:MovieClip = makeFoot(0);
  3. var footB:MovieClip = makeFoot(Math.PI);
  4.  
  5. addEventListener(Event.ENTER_FRAME, onLoop);
  6. function onLoop(evt:Event):void {
  7.      graphics.clear();
  8.      graphics.lineStyle(0,0x000000);
  9.      graphics.moveTo(footA.x, footA.y);
  10.      graphics.curveTo(footA.x + 20, footA.y - 50, 200, 100);
  11.      graphics.curveTo(footB.x + 20, footB.y - 50, footB.x, footB.y);
  12. }
  13.  
  14. function makeFoot(startAngle:Number):MovieClip {
  15.     var mc:MovieClip = MovieClip(addChild(new MovieClip()));
  16.     with(mc.graphics) beginFill(0x000000), curveTo(10,-10, 20, 0);
  17.     mc.x = 200;
  18.     mc.y = 200;
  19.     mc.sx = mc.x;
  20.     mc.sy = mc.y;
  21.     mc.t = startAngle;
  22.     mc.r = 50;
  23.     mc.addEventListener(Event.ENTER_FRAME, onRunFoot);
  24.     return mc;
  25. }
  26. function onRunFoot(evt:Event):void {
  27.     var mc:MovieClip = MovieClip(evt.currentTarget);
  28.      with(mc){
  29.         x = sx + r * Math.cos(t);
  30.         y = Math.min(sy, sy + r * Math.sin(t));
  31.         rotation = sy - y;
  32.         t += .15;
  33.      }
  34. }

This is a funny looking snippet that creates a very primitive walk cycle with the Graphics class and sine and cosine.

Posted in motion | Also tagged | Leave a comment