Internet Explorer 6 is not supported.
Upgrade to view this site.

Blog Archive

Results for April, 2009.

Defining Variables in Comma Form

Sometimes when I check out other people’s code I see things that I never do when I program. Sometimes they are kind of cool and if I like it enough, it becomes part of my workflow. While looking at some code the other day I saw something that I have never seen before. Variables defined in a comma delimited list. Below is how I normally go about defining variables.

var red:uint;
var grn:uint;
var blu:uint;
var i:Number;
var f:Number;
var p:Number;
var q:Number;
var t:Number;

The above can also be defined shorthanded using a few less lines. It’s a little thing, but you end up not rewriting the variable declaration every time this way.

var red:uint, grn:uint, blu:uint;
var i:Number, f:Number, p:Number, q:Number, t:Number;

Get Loaded SWF File Dimensions

When externally loading a SWF, the file is casted to a MovieClip object. The top left position is preserved when it loads in but you lose a few things that could be helpful. First of all your background color is gone (As far as I know). This is the color that is set in the Properties panel in Flash or the SWF meta tag in Flex. The other thing is that the dimensions aren’t preserved either. If you trace the SWF file’s width and height it will return the width and height of content inside and not the bounding of the original SWF file. But there is somewhere that this information is preserved. Inside the LoaderInfo object. To obtain the size of the loaded object just write something like this. This short example is an example of snytax and is not actual functional code.

trace(extFile.loaderInfo.width + " " + extFile.loaderInfo.height);

There is the other information that the LoaderInfo object retains like actionscriptVersion and frameRate. A full list can be found in the reference guide. Like everything else AS3.

Hex to Unsigned Integer and Back Again

Here are some quick one line wonders. From reading some stuff here and there I was able to fashion 2 functions for handling the conversion between hex strings and unsigned integers. Most of the time a uint is asked for when it comes to color, but some things like the StyleSheet object asks for a string value of a hex color like you would use in CSS.

I guess these function names got a little long, but they are very descriptive to what they do, and when you use Flex with it’s completion function name brevity isn’t as much of an issue as it is with Flash.

function hexColorToUnsignedInteger(hex:String):uint
	return uint("0x" + hex.split("#")[1]);

function unsignedIntegerToHexColor(num:uint):String
	return String("#" + (num.toString(16).toUpperCase()));


Using H.264 Encoding For FLV Files

Today I was asked to fix a problem with an existing website where a FLV was stopping quarter way in its playback. This problem only occurred in IE 7 with Windows Vista. Every other browser seemed to work fine.

First step I took was to isolate the problem. I remade the FLV from source just to see that nothing like a cue point was in there that shouldn’t be. No dice. So I tried another FLV that I knew worked and sure enough it played all the way through. From there I opened Adobe Media Encoder and started getting creative with the encoding settings.

I tried it without audio and it played all the way through. That made sense, because the one that I tested with didn’t have audio either. So the problem was now isolated to an audio problem. Some forums had vaguely familiar sounding fixes, but nothing solid that worked for me. Once again I changed bitrate and channel settings for the FLV sound. No dice. The audio options for FLV are pretty limited and I was running out of things to test for. So I thought about who does this kind of thing successfully all day; they must have a process. I figured that if anyone was encoding things right it was probably YouTube.

I checked YouTube and using this Safari trick grabbed the FLV file that YouTube generates. In my test, the YouTube video worked in IE 7 with Vista. I opened the FLV in QuickTime player. You can only do this if you have a plugin like Perian installed. I checked the movie properties. It was H.264/AAC audio. Even the CS4 Media Encoder can’t make an FLV like that. The options are only Sorenson Spark or On2 VP6. It can make a F4V this way, whatever the hell that is. In any event, that F4V had the settings that I needed, so I encoded one of those. No dice. FLV player wouldn’t play a F4V. The hillbilly in me figured I’ll just change the extension. Why the fuck not? Holy shit, it worked.

So, if you want to make an FLV file that is H.264, just make a F4V file with Adobe Media Encoder and then change the file extension.

Get a Query String From a SWF File

If you want to get a query string off a SWF file name there is a short line of code that does it. This is one of those things that I just can’t figure out logically; I just have to memorize how to do it. I’ve done this once or twice before in AS2 and it was a little bit easier then. This points to the LoaderInfo of the main SWF file. Then you get the parameters property from there. The FlashVars parameter in the SWF’s HTML page does the same thing as adding a query string to the file name.

var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;

For the SWF file name “player.swf?src=video.flv” the code to get the src value would be the following.

var src:String = paramObj["src"]; //would get "video.flv"
var srcAlso:String = paramObj.src; //same using property instead