Thursday, September 17, 2009

AS3: Getting/Keeping keyboard focus

If your application is losing keyboard focus, you can regain it by adding:

myobject.stage.focus = this;

More details in this discussion.

Sunday, September 13, 2009

AS3: Applying filter on display objects for eg. a Bitmap

Filters can be used to apply simple effects to your display object. As a example we will apply a glow filter to a Bitmap object.

The process for applying a filter to a display object is:
1. Create a new Filter object.
2. Create a temp array.
3. Copy the filters property from your display object to the temp array.
4. Add your new Filter object to the temp array.
5. Copy the temp array to the original display object filters property.

The reason for the copying out of the filters property is because AS3 does not support adding our new filter object directly to the display objects filter property.

The code will look like so:

// Parameters and default values for the GlowFilter() class are:
// GlowFilter(color:uint = 0xFF0000, alpha:Number = 1.0,
// blurX:Number = 6.0,
// blurY:Number = 6.0, strength:Number = 2,
// quality:int = 1, inner:Boolean = false,
// knockout:Boolean = false)


var _glow_filter:GlowFilter = new GlowFilter();
var _tmp_filter:Array = new Array();
var _demo_bitmap:Bitmap = new Bitmap(); // Empty, create your own bmp.

_tmp_filter = _demo_bitmap.filters;
_tmp_filter.push(_glow_filter);
_demo_bitmap.filters = _tmp_filter;

*UPDATE* (31Jan2010) : I like the tutorial here:
http://www.republicofcode.com/tutorials/flash/as3filters/

Tuesday, September 8, 2009

AS3: "include" directive to include code

The "include" directive is useful for organising code. This directive does not work like c/c++ directive, in flash it is used to inline code. So any code in the file included will be inlined where ever you specify it.

For example if you have a file constants.as residing in the same directory as your code, then the code would look like this:

Some as3 code;
include "constants.as" // Code from constants.as is inlined here.
Some more as3 code;