イナヅマTVログ

Flashで作ったswfをFlash BuilderでEmbedして使う

| 1件のコメント

Flashで作成したswfをFlash BuilderでEmbedして利用するのに右往左往したのでメモ。

Embed ClassをnewしてもインスタンスはMovieClipにならず、ByteArrayのような状態(?)になっているらしい。
byteをLoader.loadBytesを使ってloadする。
非同期イベントEvent.COMPLETEを待って処理を開始する。
あと、AIR 2, Flash Player 10.1で変更されたセキュリティ・ポリシーに対応するため LoaderContext.allowCodeImporttrueにする。

*Flash CS5 で作成したexample.swfをFlash Builder 4でEmbedして使う。
*swfのステージにインスタンス名"some_mc"のMovieClipが配置されている。

package
{
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.events.Event;
 
	public class ExampleClass
	{
		[Embed(source="assets/example.swf", mimeType="application/octet-stream")]
		private var SWFParts:Class;
 
		public function ExampleClass()
		{
			init();;
		}
		private function init ():void
		{
			var lc:LoaderContext = new LoaderContext();
			lc.allowCodeImport = true;
 
			var loader:Loader = new Loader();
			loader.loadBytes(new SWFParts(), lc);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(event:Event):void{
				var mc:MovieClip = event.target.content;
				var some_mc:MovieClip = mc.getChildByName('some_mc') as MovieClip;
				}
			);
		}
	}
}

AIR 2, Flash Player 10.1で変更されたセキュリティ・ポリシーにひっかったのが大きかった。
これが良くわからず時間がかかってしまいました。
LoaderContext.allowCodeImport はデフォルトではfalseでtrueにしないとセキュリティエラーが発生します。
SecurityError: Error #3226: Cannot import a SWF file when LoaderContext.allowCodeImport is false.

Adobeのサイトに解説がありました。
Understanding the security changes in Flash Player 10.1 and AIR 2

If the content to be loaded is a SWF file when you have set LoaderContext.allowCodeImport = false for an import operation, a SecurityError event will be dispatched, indicating error 3226:

Note that LoaderContext.allowCodeImport affects only cases that perform sandbox importing—so, for example, an ordinary Loader.load operation will always load either a SWF file or an image file, even if LoaderContext.allowCodeImport = false.

なんか超面倒くさい。
swfぐらい簡単にEmbedさせてくれたら良いのに、なんでこんな手間のかかる手順をしなきゃいけないんだ。
ライブラリにリンケージ設定されているシンボルを使うのはビックリするくらい簡単なのに。

*追記
読み込まれるswfに配置されているインスタンスにリンケージ設定するとgetChildByNameで取得できませんでした。

1件のコメント

  1. ピンバック: dispatchEventでEvent.targetがかわるよ、どうしようこうしよう | イナヅマtvログ

コメントを残す

必須欄は * がついています


このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください