Tuesday, January 5, 2010

Flash Embed getting "TypeError: Error #1007: Instantiation attempted on a non-constructor"

When use the actionscript developed in Flex, I got the following error:
TypeError: Error #1007: Instantiation attempted on a non-constructor
TypeError: Error #1007: 嘗試個體化非建構函式。

Code as following:

package
{
import flash.display.MovieClip;

public class SampleMovieClip extends MovieClip
{
[Embed(source="main_visual.png")]
private var _bmp:Class;

public function SampleMovieClip() : void
{
addChild(new _bmp());
}
}
}


It is because Flash Actionscript doesn't support Metadata "Embed", refer to following page for a solution:
http://blog.wezside.co.za/2008/04/everything-embe.html

Quick snippet:

[Embed(source="as3_pages_source.swf", mimeType="application/octet-stream")]
private var SWFBytes:Class;

private var applicationDomain:ApplicationDomain;

private function init(event:Event=null):void
{
this.loadApplicationDomain();
}

private function loadApplicationDomain():void
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderLoaded);
try{
loader.loadBytes(new SWFBytes() as ByteArray);
} catch (err:Error)
{
loader.removeEventListener(Event.INIT, onLoaderLoaded);
trace(err + ", \nLawfui noted: [Embed] is not allowed in Flash IDE, loadApplicationDomain_SWF is used instead");
loadApplicationDomain_SWF("as3_pages_source.swf");
}
}

public function loadApplicationDomain_SWF(url:String):void
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderLoaded);
loader.load(new URLRequest(url));
}

private function initPageFlipCore():void
{
core = new PageFlipCore( this.applicationDomain );
core.x = 200;
core.y += 150;

this.addChild(core);
}

private function onLoaderLoaded(event:Event):void
{
var loaderInfo:LoaderInfo = event.currentTarget as LoaderInfo;
applicationDomain = loaderInfo.applicationDomain;
initPageFlipCore();
return;
}

Starting a actionscript project with width and height set

When using Flex for a Actionscript project development, I always run into a situation that the testing movie doesn't have a prefered size, even I set the mc.width and mc.height in the script.

The following is the solution:

package
{
import flash.display.MovieClip;

[SWF(width="1000", height="628", backgroundColor="0x000000", frameRate="30")]
public class SampleMovieClip extends MovieClip
{
}
}

Thursday, April 2, 2009

Some find tunes for the amfphp settings

Trying to figure out how to sending/receiving strongly typed objects between ActionScript and AMFPHP

some find tunes needed in the following files:

globals.php
$voPath = "services/vo";
$servicesPath = "services/";


gateway.php
$gateway->setCharsetHandler("utf8_decode", "UTF-8", "UTF-8");

Thursday, April 17, 2008

Rendering Performance

I like to set the props / layout of the component/application by overriding updateDisplayList();

However, I experience a drop of rendering speed in Flex application when the application is developed into a larger size. And I discover that the key issue was using "setStyle" everytime when the updateDisplayList() is executed.

Actually the setStyle just need to apply once after the application is initialized. So the problem can be solved after I added _init flag into the application and run the setStyle procedures only if the _init is true...

Monday, April 14, 2008

wmode on Firefox

when u set wmode in html, and the page is opened in Firefox, it will mess up your special chars input such as Chinese / German or whatever... so lame...

https://bugzilla.mozilla.org/show_bug.cgi?id=347185

https://bugs.adobe.com/jira/browse/SDK-12420

http://www.google.com/search?hl=zh-TW&rls=com.microsoft%3Azh-hk%3AIE-SearchBox&rlz=1I7GZAZ&q=firefox+wmode+bug&lr=

Wednesday, February 27, 2008

Cairngorm + Modules

There are a few notes to take care:

1) For Singleton Class like DragManager which holds data, it is better to put in ApplicationDomain as RSL.

2) While using Modules, the modulesInfo needs a differnet ID even the class is the same, i.e. new another ModuleInfo even the source is the same.

3) The "Modular" Package is adopted.

For now I didn't get ServiceLocator to work as I want. Will think of adopting the Prana framework anyway. We'll see.

Tuesday, February 19, 2008

Modular Programming

Stopped flex programming for like 2 months, I am return finally.

Well... I stopped the latest development of Flex Project: [Bast] becuz I found out the file size and codes will go eventually too large for the applicant to be efficient and fluent. I was thinking about making some component as smaller part application and load it into main application container whenever it is needed. But never figure it out... and finally I get to read part of the Modular Programming notes.

And I plan to try out the following:
1) Program modules <-- can apply framework
2) Link the Progress event against preloader of DisplayObjectContainer or ProgressBar Control

Just a simple concept, let's see if it works or not.

On the other hand, I participated in Adobe FMS3 seminar this week, seems the price of FMS3 is kinda cheap nowadays.

Wednesday, November 7, 2007

Use customize Style when initializing the app

I read this somewhere, if you want to use your own style while the app is initializing, you need to use initialize='onInitialize()'; when the application is initializing...

Tuesday, November 6, 2007

Make sure your CSS stylesheet is loaded before using measureWidth

I ran into a situation that when I apply a style class and need to retrieve the measuredWidth or textWidth for calculating component's properties.

I figured out we need to use validateNow(); to update the component's properties.
However, somehow the figures are kinda different on same component.

When I look close at the debugger, I found this line which is suspective:


Execute event: [Event type="" bubbles=false cancelable=false eventPhase=2]
animals: 57,26
[SWF] assets\css\main.swf - 3,196,972 bytes after decompression
Execute event: [Event type="" bubbles=false cancelable=false eventPhase=2]
animals: 73,28


I found that the Style sheet are not loaded before I moved to next step for checking measuredWidth, and here is the official solution:

var myEvent:IEventDispatcher =
StyleManager.loadStyleDeclarations('assets/css/main.swf');
myEvent.addEventListener(StyleEvent.COMPLETE, onLoadComplete);

Friday, October 26, 2007

A filed weird behaviour - The component doesn't render correctly after adding to Stage

Apart from sharing Flex Programming experience, this blog also serve as a record or notes for some weird scenerios...

There were once I working on a slide show project. Basically a container is moving along the y-axis and thumbnails are adding dynimatically to the container. But somehow the thumbnails are not rendered on screen. The codes are executed as I debug'd them and does show the lines.

The thumbnails show up if I stopped the moving container. This is weird to me.

Finally, I found a dirty solution, i.e. add the thumbnails container to rawChildren.

It seems the issue is related to the render sequence of rendering...
e.g. Initialize component -> set properties -> updateDisplayList -> commitProperties and so on...

Wednesday, October 17, 2007

Set the verticalScrollPosition

Sometimes we want to set the scrollerbar position by actionscript, that can be easily achieved by:
verticalScrollPosition = maxVerticalScrollPosition;
however, this won't work right after we added some childs to a container.
The flash player need to refresh the changes in order to calculate the right position.

To move to the right position, you may try this:
validateNow();
verticalScrollPosition = maxVerticalScrollPosition;

Tuesday, October 16, 2007

Add Listeners to ImageProxy used in mx.managers.DragManager

Ran into another problem on my "Houngan" Project.

I want to drag an Image using DragManager.
And I applied imageProxy in the doDrag function and it works fine.

However, I want to hide the original image, which remains at the position it located, when I dragging the imageProxy.
So I tried the following steps:


  1. click on the original image, broadcast mouse down event
  2. execute the onMouseDown function, hide the image.
  3. make an imageProxy
  4. execute DragManager.doDrag function and move the imageProxy by following
DragManager.doDrag(initiator, ds, event, imageProxy, 0, 0, 1, true);

Yes I can hide the original image and drag the imageProxy, but the release of imageProxy doesn't resume the original image's visible stage. There is no event broadcasted from DragManager on this as far as I know. (Please kindly tell me if you do know one ^^)

I went over the source code of DragManager and discover there is a imageProxy variable in DragManagerImpl.as
And whenever endDrag is called, the imageProxy is removed from the parent container. So there is a way:


  1. click on the original image, broadcast mouse down event
  2. execute the onMouseDown function, hide the image.
  3. make an imageProxy and add event listener to it for the Event.REMOVED event.
  4. execute DragManager.doDrag function
  5. execute listener handler when Event.REMOVED is detected from imageProxy.

So that's the trick =)

Friday, October 12, 2007

Memory Leak in Flex

Here comes my first case study: Memory leak by using Bitmap & BitmapData.

Currently I cope with a project which need to load external JPEGs into Flex and forming BitmapData objects. I will do some operations on the BitmapData, then put it into the Application.

During the application runtime, I will remove those bitmap display objects, but I discover the garbage collection doesn't really release memory from the application, so there is a memory leak. (The memory used up to 350M after I afk 30mins)

I study gskinner's
AS3: Resource Management pt 2, and carry out some test as following:

private function onBitmapLoadComplete(event:Event):void
{
var bitmapDataProxy:BitmapDataProxy = event.currentTarget as BitmapDataProxy;
bitmapDataProxy.removeEventListener(BitmapDataProxy.LOAD_COMPLETE, onBitmapLoadComplete);

var bitmap:Bitmap = new Bitmap(bitmapDataProxy.result.clone(), "auto", true);

var bitmapContainer:UIComponent = new UIComponent();
bitmapContainer.addEventListener(MouseEvent.CLICK, onMouseClick, false, 0, true);
bitmapContainer.addChild(bitmap);
this.addChild(bitmapContainer);

bitmapDataProxy.destroy();
bitmapDataProxy = null;
bitmap = null;
bitmapContainer = null;
}

private function onMouseClick(event:MouseEvent):void
{
var bitmapContainer:UIComponent = event.currentTarget as UIComponent;
bitmapContainer.removeEventListener(MouseEvent.CLICK, onMouseClick);

bitmap.bitmapData.dispose();
this.removeChild(bitmapContainer);
bitmapContainer = null;

bitmap=null;
}

Checked with Flex Profiler, the Memory usage up to 4279k at the loading of BitmapDataProxy.
After the loading is complete, I have trashed the BitmapDataProxy after putting the BitmapData into a Bitmap. This can be observed by observed used memory dropped to 1666k after I press the garbage collection button in Profiler. However, weird stuff came...

After I clicked on the image and removing the child. The used memory doesn't further drop down. And by observing Loitering Objects, I see there are still a bitmap object and BitmapData in the memory. So probably I can't clear it.

By further test, I found that The BitmapData left in memory is referring to the property of Bitmap, i.e. Bitmap.bitmapData. I have clear it, but I need to remove the reference in order to release the counter of reference to the object in memory.

By adding the code, I further dropped the used memory to 1141k. And there are no more BitmapData instance in the memory. but there is still one Bitmap instance. so I guess the removeChild doesn't really remove all the childs in the Display List.

bitmap.bitmapData.dispose();
bitmap.bitmapData = null;


Finally I added following code to remove the unused Bitmap instance:

bitmapContainer.removeChild(bitmap);

I finally can remove all the Memory Leakage problem in this little revising practice. I think there are much more to go over my Album Project, with project coded "Houngan".

Uhmmm, at least I see hope through this practice. And wish it helps you too.

The Finalized code as following:

private function onBitmapLoadComplete(event:Event):void
{
var bitmapDataProxy:BitmapDataProxy = event.currentTarget as BitmapDataProxy;
bitmapDataProxy.removeEventListener(BitmapDataProxy.LOAD_COMPLETE, onBitmapLoadComplete);

var bitmap:Bitmap = new Bitmap(null, "auto", true);
bitmap.bitmapData = bitmapDataProxy.result.clone();

var bitmapContainer:UIComponent = new UIComponent();
bitmapContainer.addEventListener(MouseEvent.CLICK, onMouseClick);//, false, 0, true);
bitmapContainer.addChild(bitmap);
this.addChild(bitmapContainer);

bitmapDataProxy.destroy();
bitmapDataProxy = null;
bitmap = null;
bitmapContainer = null;
}

private function onMouseClick(event:MouseEvent):void
{
var bitmapContainer:UIComponent = event.currentTarget as UIComponent;
bitmapContainer.removeEventListener(MouseEvent.CLICK, onMouseClick);

var bitmap:Bitmap = bitmapContainer.getChildAt(0) as Bitmap;

bitmap.bitmapData.dispose();
bitmap.bitmapData = null;

bitmapContainer.removeChild(bitmap);

this.removeChild(bitmapContainer);
bitmapContainer = null;

bitmap=null;
}

Thursday, October 11, 2007

Finally ... my first post!


Hi all, I decided to register an account and make it up finally.
The purpose of this blog is to record my Flex Learning Career, and share some of the experiences.

I am a Multimedia Engineer and the company I worked starting to adopt Adobe Flex as the solution against various projects.

Personally I start to study Adobe Flex when it is released at beta 2.0, and wrote some stock quoting program. The functions are simple but still somehow I feel good with it.

When I go deeper with it, I started to find there are many problems to deploy corporate-wise solutions with Flex. So I started to study Cairngorm, which is a Micro Architecture Programming Framework initialized by Steven Webster and his colleagues.

The Cairngorm improved my production speed a lot, with the help of Cairngen...


I found that there are many place a Flex Engineer need to be careful, for example, memory leak, which may continously consume the resource of client's computer and eventually hang user's computer. I will post more in the coming days.

haha... Got to go for lesson, bye~

//testing of posting code
var foo:Object = new Object();
foo.name = 'camus';