Flashで作成したswfをFlash BuilderでEmbedして利用するのに右往左往したのでメモ。
Embed ClassをnewしてもインスタンスはMovieClipにならず、ByteArrayのような状態(?)になっているらしい。
byteをLoader.loadBytes
を使ってloadする。
非同期イベントEvent.COMPLETE
を待って処理を開始する。
あと、AIR 2, Flash Player 10.1で変更されたセキュリティ・ポリシーに対応するため LoaderContext.allowCodeImport
を true
にする。
*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, aSecurityError
event will be dispatched, indicating error 3226:Note that
LoaderContext.allowCodeImport
affects only cases that perform sandbox importing—so, for example, an ordinaryLoader.load
operation will always load either a SWF file or an image file, even ifLoaderContext.allowCodeImport = false
.
なんか超面倒くさい。
swfぐらい簡単にEmbedさせてくれたら良いのに、なんでこんな手間のかかる手順をしなきゃいけないんだ。
ライブラリにリンケージ設定されているシンボルを使うのはビックリするくらい簡単なのに。
*追記
読み込まれるswfに配置されているインスタンスにリンケージ設定するとgetChildByName
で取得できませんでした。
ピンバック: dispatchEventでEvent.targetがかわるよ、どうしようこうしよう | イナヅマtvログ