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;
}

No comments: