Author Archives: Zevan

Function Returns a Function

CLICK HERE TO COPY
Actionscript:

appendExclamation("actionsnippit")("dot")("com")("is")("live");

 

function appendExclamation(str:String):Function{

  trace(str + "! ");

  return appendExclamation;

}

 

/* outputs:

actionsnippit!

dot!

com!

is!

live!

*/

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. [...]

Posted in functions, return values | Comments closed

Sine Cosine Walk

CLICK HERE TO COPY
Actionscript:

stage.frameRate = 30;

var footA:MovieClip = makeFoot(0);

var footB:MovieClip = makeFoot(Math.PI);

 

addEventListener(Event.ENTER_FRAME, onLoop);

function onLoop(evt:Event):void {

     graphics.clear();

     graphics.lineStyle(0,0x000000);

     graphics.moveTo(footA.x, footA.y);

     graphics.curveTo(footA.x + 20, footA.y - 50, 200, 100);

     graphics.curveTo(footB.x + 20, footB.y - 50, footB.x, footB.y);

}

 

function makeFoot(startAngle:Number):MovieClip {

    var mc:MovieClip = MovieClip(addChild(new MovieClip()));

    with(mc.graphics) beginFill(0x000000), curveTo(10,-10, 20, 0);

    [...]

Posted in motion | Tagged , | Leave a comment

const

CLICK HERE TO COPY
Actionscript:

const HALF_PI:Number = Math.PI / 2;

const RADS:Number = Math.PI / 180;

 

trace(HALF_PI);

// convert degrees to radians

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 [...]

Posted in variables | Tagged , | Leave a comment

KeyCode Variables

CLICK HERE TO COPY
Actionscript:

// create constants for all letter and number keys:

var alphabet:Array = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");

var nums:Array = ["ZERO","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE"];

 

var key:Object = new Object();

for (var i:int  = 0; i<alphabet.length; i++)

     key[alphabet[i]] = 65 + i;

     

for (i = 0; i<nums.length; i++){

     var code:int = 48 + i;

     key[nums[i]]= code;

     key[i] = code;

}

  [...]

Posted in Object, keys | Tagged , , | Leave a comment