Using Flash Components in Flex Builder

I ran into this again today and had to think it out, so figured it might help some folks.

If you are looking for the fl.controls.ComboBox or other fl.controls code in Flash Builder, you will find it conveniently absent.  The easiest way I have found to make available the code for the controls I want to use in a Flash project built in Flex Builder is to create a Components.fla file and then compile it out to a .swc.  You can then include the

1. Create an empty flash file called Components.fla

2. In the publish settings of your flash file, click the Flash tab, then check the Export SWC box.

3. Drag the components you want to use in your project to the stage (don’t worry, you can come back later and add more if you need to).

4. Save your file, then File>Publish.

5. In Flex Builder, right click on your project folder and go to Properties.

6. Click Actionscript Build Path on the left column, then the Library Path tab.

7. On the library path tab, click Add SWC and browse to the SWC file you just created.

Poof, now you have Flash Component code available in your Flex environment for code hinting, etc.

True Random Integers in Flash AS3

So many people have this wrong, I have to post something right about this. I’m going to show you the example below and explain to you why it’s WRONG, and the simple change in logic required to make true random number generation happen for you.

THIS IS WRONG
var myRandomNumber:Int = Math.floor(Math.random()*(High-Low))+Low;

For example, if you wanted a number between 2 and 10:

var myRandomNumber:Int = Math.floor(Math.random()*(10-2))+2;

You will never get 10 as an answer.

Here is why:
The Math.random() function in AS3 returns a pseudo-random number, where 0 <= n < 1. This means you will never get 1 as your result of calling Math.random(). Without 1, you will never get any number greater than 10 to round down to 10. Everybody and their mom has the above formula as the 'correct' way to get a ranged integer number out of the Math.random() function.

So why not use Math.ceil, or Math.round? Math.ciel will give you the same problem, but at the beginning of your number range. Math.round sounds great, except you'll be left with 50% less chances to receive either your top or bottom number.

So what's the solution?

Easy. Add 1 inside your Math.floor call before you multiply with your random number. Then everything will have the same odds.

THIS IS RIGHT
var myRandomNumber:Int = Math.floor(Math.random()*(1+High-Low))+Low;

Think about it. Use it. And if there is a better way to do this, let me know. If I’m wrong, please tell me. I love to know when I’m wrong.

Using fl.* library within Flex Builder 3 for Mac

Why this isn’t set up to work by default for Actionscript Projects is beyond me, but I found a need to use the fl.color Class this week in Flash and it sure was a challenge to get the fl.* libraries to work natively within Flex Builder 3 for Mac.

Here is how you do it:

1. Right click on your project folder in Flex, click “Properties.”

2. Click on the “Actionscript Build Path” menu item.

3. Click “Add Folder”

4. Navigate to: /Applications/Adobe Flash/Common/Configuration/Actionscript 3.0/projects/Flash/src/

5. Click “Choose” to select the /src/ folder and “Ok” to add this to your source path.

You can do the same thing for Windows based Flex Builder as well, the file path should be similar to this:
C:\Program Files\Adobe\Adobe Flash CS3\en\Configuration\ActionScript 3.0\Classes

P.S. Speaking of fl.color, fl.color.setTint() is a great trick to tinting without having to deal with the more complex colorTransform class.

Capacity rebrands Cartoon Network

For possibly the most fantastic and satisfying work that you may see all year, check out Capacity and their visual rebrand of Cartoon Network.

Imagine animated Kidrobot vinyl dolls, with Cartoon Network skins and you will know what I’m talking about.

Vista Gadget Downloading as a Zip? Here’s how to fix it.

Apparently Internet Explorer is REALLY smart. It knows that .Gadget files are REALLY .zip files. And since by default the Apache web server doesn’t know what kind of MIME headers to send for a .Gadget file, it doesn’t send any and IE has to figure it out. (More than likely IIS takes care of this automagically, so this fix is for all Apache based sites)

This generally results in a customer downloading a .zip file that they have no idea what to do with.

And apparently there isn’t much if any documentation on how to get around this problem. It is easy.

Create an .htaccess file, put it in the root of your website folder, and add the following line of text:

AddType application/x-windows-gadget Gadget

That’s it.

Enjoy.

levelHead – Spatial Reality Game


levelHead v1.0, 3 cube speed-run (spoiler!) from Julian Oliver on Vimeo.

 

This has to be one of the more inspiring things I’ve seen lately.  Very cool use of video technology and 3d perspective.  Bravo.

Flash AS3 – CharCodeStrings Library (download)

A few weeks ago I was working on an interesting text typing widget for our new website (that isn’t up yet, still in the works). I came to find that there was a Keyboard.CharCodeStrings Array Constant in the AS3 Language reference–but it was only available within the AIR 1.0 runtime. Bummer.

So like many folks out there, I needed to roll a homegrown solution to match up CharCode result numbers with their character counterparts. Essentially a singleton class that you can use when you catch your event and turn the resulting CharCode into a literal string character.

Here is an example:

package {
	import flash.events.KeyboardEvent;
	import flash.display.MovieClip;
	import com.iq.CharCodeStrings;
	public class CharCodeStrings_sample extends MovieClip {
 
		public function CharCodeStrings_sample() {
			stage.addEventListener(KeyboardEvent.KEY_DOWN, traceKey);
		}
		public function traceKey(event:KeyboardEvent):void {
			var myChar:String = CharCodeStrings.getChar(event.charCode);
			trace(myChar);
		}
	}
}

If this may be useful to you, there is a download link below. I built this to do what I needed for the project I was working on, so the character codes returned are for US English–but you could easily substitute another language by using the included sample app and replacing the characters. If you do set this up for use in another language feel free to shoot me the file and I will link it here.

Download CharCodeStrings

(Hint, when testing from within the Flash IDE turn off Keyboard Shortcuts when you run your movie to test or the preview window may not receive keyboard input. It is in the Control menu when your swf is playing.)

Google Maps API for Flash!

Looks like Google is releasing a Flash API for Google Maps.

This should be something fun to play with down the road.

Google Maps API @ Google Code

What’s changing in PHP 6.0?

There is an interesting article up today about what major changes will be made in the upcoming release of PHP V6. Most of the changes won’t affect anybody developing with the current standards established with recent PHP V5 releases

New Features

  • Namespaces – Should be great for integrating libraries with third party code.
  • Soap – Now Standard instead of an optional compile-in feature.
  • XML – Now Standard instead of an optional compile-in feature.

Things Removed
These items are going to make a lot of standard open-source apps fall apart.

  • magic_quotes
  • register_globals
  • register_long_arrays
  • safe_mode

Read the article here >

Hello world!

Seems cliché, but doesn’t everything new start out this way??