By Zevan | October 28, 2008
Actionscript:
-
var alphabet:Array = ("abcdefghijklmnopqrstuvwxyz").split("");
-
trace("this is the letter b...", alphabet[1]);
-
trace(alphabet);
-
/* outputs:
-
this is the letter b... b
-
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
-
*/
This is an easy way to create an array of the alphabet. It's easier to type than:
Actionscript:
-
var alphabet:Array = ["a","b","c".... etc];
By Zevan | October 28, 2008
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.
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....
By Zevan | November 30, 1999
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);
-
mc.x = 200;
-
mc.y = 200;
-
mc.sx = mc.x;
-
mc.sy = mc.y;
-
mc.t = startAngle;
-
mc.r = 50;
-
mc.addEventListener(Event.ENTER_FRAME, onRunFoot);
-
return mc;
-
}
-
function onRunFoot(evt:Event):void {
-
var mc:MovieClip = MovieClip(evt.currentTarget);
-
with(mc){
-
x = sx + r * Math.cos(t);
-
y = Math.min(sy, sy + r * Math.sin(t));
-
rotation = sy - y;
-
t += .15;
-
}
-
}
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 actionscript, flash |
By Zevan | November 30, 1999
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 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:
-
// inside a class
-
public static const RENDER_COMPLETE:String = "renderComplete";