Category Archives: Flash

Setting stage size for an Actionscript Project in Flex Builder / Flash Builder

Since this is a little documented “feature” (or so I think) I figured I would stick this here is a note and reference for myself.

When working in Flex Builder / Flash Builder there is no project property to set stage size, or background color, etc.

But if you place the following code into your class structure–after the includes, before your constructor–it will set the default dimensions, background color, and frame rate of your application:

[SWF(width='800',height='600',backgroundColor='#ffffff',frameRate='25')]

This is most useful when building Air apps when you want to make use of the default application window and have your default .as or .mxml file size set properly to fill that space.

Flash Builder 4 – Profiling won’t start for my Air App!

So I ran into an interesting problem today that I couldn’t find any information on out there for. I still don’t have a concrete idea of what is going on, but I do have a workaround.

This afternoon I was trying to launch the Profiler from within Flash Builder 4 to start looking at memory management optimization of the desktop application we are currently developing. To my surprise, when clicking “Profile Application,” the application would launch in adl but FB4 would not switch to the Flash Profile view.

I spent a couple of hours rebuilding the project files from scratch, but still no dice. Through all my testing I could build any number of new empty air projects and they would all build and profile properly.

But not my code.

So I created a new default app, launched with profiler…. And it worked! (But no code, of course).

It turns out that, for some reason, the initialization code for my application seems to be bogging down ADL so much that it is unable to connect with the profiler on launch.

My solution to the problem was to add a 1 second timer that would slow down the initialization of my application until the Profiler was able to connect–and it worked.

Strange workaround to a strange problem. If anybody else has any ideas, let me know.

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.

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