[Modules] Check if class implements interface

October 2nd, 2011 by damian No comments »

Problem:

I have module that implements my interface:

class MyModule extends ModuleBase implmenets IMyInterface
{
...
}

After loading module from external file:

var moduleInfo:IModuleInfo = ModuleManager.getModule('module_path.swf');
moduleInfo.addEventListener(ModuleEvent.READY, this_READY );
moduleInfo.load(null,null,null ,FlexGlobals.topLevelApplication.moduleFactory);

And creating module object:

private function this_READY( moduleEvent:ModuleEvent ):void {
var moduleInfo:IModuleInfo = moduleEvent.module;
var m:Object = moduleInfo.factory.create();
}

The expression:

m is IMyInterface;

returns false instead of true.

 

Solution:

The solution for this problem is following:

After creating the module object using factory, we must use the expression presented above. I do not know why, but after the first use of this expression or casting (operator as) it behaves right.

Therefore we must add the following line:

private function this_READY( moduleEvent:ModuleEvent ):void {
var moduleInfo:IModuleInfo = moduleEvent.module;
var m:Object = moduleInfo.factory.create();

m is IMyInterface;
}

It looks like error in Flex for me, as I cannot find an explanation for it.

Two way data binding

September 23rd, 2011 by Piotr Wierzgała No comments »

If you bind a variable to some component’s property by just adding [Bindable] metatag above it you may notice that component’s property changes when variable changes but it doesn’t work in the opposite direction. So what to do in order to achieve to way data binding? » Read more: Two way data binding

png2swf: a way to keep transparency and small PNG file size

August 7th, 2011 by Piotr Wierzgała No comments »

png2swf utility is a part of SWFTools package. It is capable of converting PNG to SWF format and it can really save your life if you are in need of having small PNG files. » Read more: png2swf: a way to keep transparency and small PNG file size

BitmapData and transparency

August 4th, 2011 by Piotr Wierzgała No comments »

If you used draw method from BitmapData class to copy some DisplayObject with transparency you may notice that all transparent pixels turned white. This is because you didn’t set properly parameters in BitmapData construcotr.

BitmapData(width:int, height:int, transparent:Boolean=true, fillColor:uint=0xffffffff)

By default BitmapData fills it’s background with white and to avoid this you have to set fillColor parameter to any transparent color.

var bitmapData:BitmapData = new BitmapData(someWidth, someHeight, true, 0x00ffffff);
bitmapData.draw(someDisplayObject);

ByteArray to BitmapData. Loading image in SWF format.

August 4th, 2011 by Piotr Wierzgała No comments »

If you load an image saved in SWF format and you would like to have it as BitmapData then the following three functions will solve the problem. » Read more: ByteArray to BitmapData. Loading image in SWF format.

ByteArray to BitmapData. Loading image with FileReference.

August 3rd, 2011 by Piotr Wierzgała 1 comment »

If you load a graphic file with the FileReference class you will get it as a ByteArray and you will probably like to convert it to BitmapData. » Read more: ByteArray to BitmapData. Loading image with FileReference.

FlexPrintJob, TypeError: Error #1009: Cannot access a property or method of a null object reference.

August 3rd, 2011 by Piotr Wierzgała No comments »

Full error text:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at mx.printing::FlexPrintJob/addObject()[E:\dev\hero_private\frameworks\projects\framework\src\mx\printing\FlexPrintJob.as:252]

What caused the warning?
I was trying to print dynamically created image. The error occured in function addObject from FlexPrintJob class.

What is the solution?
You have to add printed object to a stage before you add it to FlexPrintJob.

var flexPrintJob:FlexPrintJob = new FlexPrintJob();
addElement(image);
if (flexPrintJob.start() != true)
{
    removeElement(image);
    return;    
}
flexPrintJob.addObject(image, FlexPrintJobScaleType.SHOW_ALL);
flexPrintJob.send();
removeElement(image);

Note that the printed object will not be visible on a stage even for a second because it’s immediately removed from the stage after sending to FlexPrintJob.

Nested inner functions as event listeners

August 3rd, 2011 by Piotr Wierzgała No comments »

Sometimes it happens that you need to pass some paremeters to you event listener function but because it can’t be done you declare additional class fields specially for this purpose. Let’s consider the following sitiation: » Read more: Nested inner functions as event listeners

How to get RGB color values from BitmapData?

July 30th, 2011 by Piotr Wierzgała No comments »

BitmapData object has two methods: getPixel() and getPixel32 which return hexadecimal color value without and with alpha channel respectively. You can easily extract RGB values from hexadecimal notation using the code below:

var pixel:uint = bitmapData.getPixel32(x, y);
var alpha:uint = pixel >> 24 & 0xff;
var red:uint = pixel >> 16 & 0xff;
var green:uint = pixel >> 8 & 0xff;
var blue:uint = pixel & 0xff;

How to disable mouse events on transparent parts of PNG image?

July 30th, 2011 by Piotr Wierzgała No comments »

Transparent area in vector graphics doesn’t generate any mouse events simply because it holds no data. In case of raster graphics every transparent area is a solid block of pixels with aplha channel equal zero. All transparent pixels in a PNG file behave like non-tranparent ones what sometimes can be treated as unwanted feature. » Read more: How to disable mouse events on transparent parts of PNG image?

Flexmaniaks on Facebook