{1 note}

this is so beautifully done

it’s logical, elegant, and when you explain it to me, I can work with it

— 

Adam, giving feedback on the project I did for him.

We were both wary of working together on this project. And actually, I’d turned it down, but he couldn’t find anyone else to do it so the guilt kicked in and here we are.

This makes everything—the fights, the late nights, the stress—completely worth it. It reminds me that while I sometimes dream of leaving this field and becoming a teacher, I’m right where I’m supposed to be.

Second AS3 Snippet of the day…

{0 notes}

A coworker and I drove ourselves halfway to insanity trying to figure out why the number Powerpoint was spitting out as a color wasn’t translating correctly when we used it in Flash.

I vaguely remembered something about the number being a decimal representation of the RGB values instead of the hex values, so after much Googling and translating from VB to AS3, I give you:

function decToColor(dec:Number):Number {
	var red:int = Math.floor(dec & 0xFF);
	var green:int = Math.floor((dec & 0xFF00) / 256);
	var blue:int = Math.floor(dec / 65536);
	
	return red<<16 | green<<8 | blue;
}

AS3 Tip

{0 notes}

With AS3.0, we’ve been given a pretty cool new method for the TextField class: getCharBoundaries. This method returns the bounding box (in the form of a Rectangle) for a specified character in the TextField.

Here’s a way to animate a TextField to make it look as though it’s being typed, letter by letter, onto the stage:

/**
 * Create a "typing" effect on a TextField instance
 * @param	myRoot		the "root" displayObject which will parent the mask (should be the same as your TextField's parent)
 * @param	txtField	the instance of TextField to be animated
 * @param	time		the total amount of time allotted for the animation (leave blank for the default)
 */
public static function animateText(myRoot:DisplayObjectContainer, txtField:TextField, time:Number = 0):void {
	// If no time is specified, default to 0.1 second per character
	if (time == 0) time = (txtField.length * 100) / 1000;

	var txtMask:Sprite = new Sprite();
	txtField.mask = txtMask;
	// You should probably start your TextField out hidden in some way
	txtField.alpha = 1;
	txtField.visible = true;

	txtMask.x = txtField.x;
	txtMask.y = txtField.y;
	myRoot.addChild(txtMask);

	var g:Graphics = txtMask.graphics;
	g.lineStyle(0, 0, 0);

	/* Set up a timer to perform the "animation." Find out the length of time 
	 * for each letter by dividing the provided time by the length of the field */
	var maskAnim:Timer = new Timer((time * 1000)/txtField.length, txtField.length);

	var letterRect:Rectangle;
	maskAnim.addEventListener(TimerEvent.TIMER, function(e:TimerEvent) {
		// For each letter in the TextField, draw a rectangle on the mask
		letterRect = txtField.getCharBoundaries(maskAnim.currentCount-1);
		g.beginFill(0, 1);
		g.drawRect(letterRect.x, letterRect.y, letterRect.width, letterRect.height);
		g.endFill();
	});

	maskAnim.addEventListener(TimerEvent.TIMER_COMPLETE, function(e:TimerEvent) {
		// Once the animation is complete, remove and dispose of the mask
		txtField.mask = null;
		myRoot.removeChild(txtMask);
		txtMask = null;
	});

	maskAnim.start();
}

SOH-CAH-TOA

{Notes}

Every once in a while, I get a chance to brush off my math skills and use them at work.

Today, for example, I had to place a bunch of MovieClips vertically along a 3-degree angle.  At first, I thought I was going to have to place them manually on the stage using the Flash IDE, but then something inside of me said “Why don’t you just use trigonometry?” followed by an even louder “SOH-CAH-TOA!!!”

I’ll be honest, aside from the Pythagorean theorem, that mnemonic device is the only thing that’s left from my trig class. I can tell you what it stands for, but I’ll be damned if I remembered exactly how to apply it.  That’s where Wikipedia comes in:

Tangent function, defined as the ration of the opposite leg to the adjacent leg.

Here’s the code if you’re interested:

var numItems:uint = 20; //the number of items you need to place

var currY:Number = 0; //keep track of the vertical axis

var deg:Number = 3; //The degree of the angle



for(var i:uint = 0; i < numItems; i++){

     var currMC:MovieClip = new MovieClip();

     /* basically, we're solving for the opposite side, given the adjacent side

        and the Tangent of the angle */

     currMC.x = Math.tan(deg*(Math.PI/180)) * currY;

     currMC.y = currY;

     currY += currMC.height + 10;

}

{Notes}

// stick the text in the measuring TextField and let it flow baby

— 

mx.controls.alertClasses.AlertForm class, line 185

I was in there trying to figure out why the hell the AS2 Alert component always cuts off text.