Quick Alphabet Array

Actionscript:
  1. var alphabet:Array = ("abcdefghijklmnopqrstuvwxyz").split("");
  2. trace("this is the letter b...", alphabet[1]);
  3. trace(alphabet);
  4. /* outputs:
  5. this is the letter b... b
  6. a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
  7. */

This is an easy way to create an array of the alphabet. It's easier to type than:

Actionscript:
  1. var alphabet:Array = ["a","b","c".... etc];

Posted in arrays, string manipulation, strings | 3 Comments

Function Returns a Function

Actionscript:
  1. appendExclamation("actionsnippit")("dot")("com")("is")("live");
  2.  
  3. function appendExclamation(str:String):Function{
  4.   trace(str + "! ");
  5.   return appendExclamation;
  6. }
  7.  
  8. /* outputs:
  9. actionsnippit!
  10. dot!
  11. com!
  12. is!
  13. live!
  14. */

This technique allows you to call a function more than once on one line. I first saw this used in the source files from a talk at flashcoders ny... I didn't attend the talk, but stumbled on this interesting blog post awhile back http://www.flashcodersny.org/wordpress/?p=166.

This is a fun technique to use, I've found some unusual uses for it already... some of which you'll see in later posts....

Posted in functions, return values | Comments closed

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 | Tagged , | Leave a comment

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 | Tagged , | Leave a comment