Archive for the ‘ActionScript’ category

[Modules] Check if class implements interface

October 2nd, 2011

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.

BitmapData and transparency

August 4th, 2011

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

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

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.

Nested inner functions as event listeners

August 3rd, 2011

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

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

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?

How to get script execution time?

July 16th, 2011

If you like to know how long it takes to execute your script the code below will print number of consumed milliseconds:

var a:int = getTime();
//your script here.
Alert.show((getTime()-a).toString());

How to replace all matches of some pattern in a string?

January 1st, 2011

When you want to replace all matches of some pattern in a string you may think that replace function will do the thing but unfortunately you will find out very soon that it replaces only the first match.

To replace all matches of a pattern you should use replace function with a regular expression:

string = string.replace(/ /g, ",");

, where “g” is one of the flags that can be set with regular expressions and it means that a regular expression will be applied to all possible matches – not only to the first one.

XML: filtering by attribute or element value

December 27th, 2010

While working with XML structures I often needed to get a node described by some specific property. I had used to travese the XML structure with for each loop until I found out that there is much better way to achive the goal with just one line of code. To not multiply entities beyond necessity I enclose a reference to a source: Traversing XML structures. The solution I was writting about can be found in section: “Filtering by attribute or element value“.

Flexmaniaks on Facebook