Wednesday, January 7th, 2009

Archive for the ‘Flash’ category

_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!

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!

Further adventures in colorspaces

Update: Here's a slightly modified version of the colorspace adventure! In this iteration, you can also adjust sliders for R, G, and B. Regardless of which "space" you make changes to, the other space will update accordingly:

(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)

If you read my last entry, you know I'm in the process of reinventing the wheel! I mean creating a color picker.

In preparation, I did some additional research on the HSV colorspace. The Wikipedia article contains all kinds of totally awesome math stuff -- some of which I actually understand! (I also found another site that, while replete with pictures and explanations, etc., left me scratching my head, though I think if I read through it again, I could figure it out...)

Anyway, after looking over the formulae (and only understanding a handful of them), I created the following thingy:

(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)

Either drag the HSV sliders around or click the color chip in the lower left corner to modify the displayed color. The HSV values are (roughly) reflected in fields next to their respective sliders; the RGB values are displayed in fields to the right of the color chip in decimal, hexadecimal, and percentages.

So what's the point? I'm not entirely sure. I guess in a lot of ways, I think HSV is a more intuitive method for choosing a color than RGB, and the ultimate purpose for this color picker was (is?) a "game" for the unwashed (read: non-designer-y) masses My previous experiments (conducted back in the olden days) were coarse hacks compared to this iteration -- although, in fairness, they (mostly) worked, even if somewhat "particular." I guess I'm just excited to see that after all these years, I'm still capable of learning and improving. Or something.

If you want the source, it's right here!