Wednesday, January 7th, 2009
_root._url isn’t as dead as I thought!

I'm probably the last person to figure this out, but just in case I'm just second-to-last, here's something I just figured out: In AS2, you could use _root._url to figure out the name of your SWF. In many of the projects on which I work, such functionality was precious! I was sad to lose it with AS3...until today!

That's right! While updating a class for use in AS3 projects, I needed an alternative to _root._url. A quick visit to Flash's help files (I know!) revealed the following:

myDisplayObject.loaderInfo.url

How is it that I've never seen nor heard about that until just now?! Ack!

Well, there it is! Now all of us know how to get the filename of our SWF!

Pumpkin stuff

Tonight, my wife (and our neighbors) carved a few jack o'lanterns (however it's punctuated). After searching in vain for almost forever (read: seven or eight minutes) for a totally sweet dragon head or something, I decided I'd tackle vintage, old school 8-bit NES sprites.

What a kick in the pants that turned out to be. I was originally going to attempt Warmech from Final Fantasy I. But he had too many colors (4, I think) and too many pixels. My second choice was Mario from Super Mario Brothers 3. That jerk has eyes in the middle of his face! (Because of the nature of carving with negative space, anything "white" gets tossed; anything gray or black remains.

This was our first time toying with varying levels of opacity: in hindsight, I should have scraped more material from the inside of the pumpkin before hacking and slashing; that said, however, I think the result is a moderate success!

Gooma o'lantern

Key.isDown is gone!

Those of you familiar with ActionScript 1.0 (and even ActionScript 2.0) may have, on occasion, employed the ever-so-helpful flag Key.isDown when testing for the "downness" of a key.

In Actionscript 3.0, you no longer have that luxury!

Here's what I've been doing to fake it:

import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
 
var keys:Object=new Object();
 
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyboardDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyboardUp);
 
function onKeyboardDown (evt:KeyboardEvent):void
{
keys[evt.keyCode]=evt.keyCode;
}
 
function onKeyboardUp (evt:KeyboardEvent):void
{
delete keys[evt.keyCode];
}

Then, if you need to test for key combinations, add to the relevant handler; for example, if I wanted to make sure the Ctrl, W, and Z keys were all currently down, I'd add this to the onKeyboardDown listener:

if (keys[Keyboard.CONTROL] && keys[87] && keys[90]) {
// do stuff
}

(87 is the key code for "W," and 90 is the key code for "Z". Don't want to memorize key codes? There's always this handy-dandly little thingy!)

It's pretty straightforward, which is probably why lots of people have already written their own routines for handling Key.isDown. If you're one of the ones who hadn't written such statements, feel free to steal them from here!