getDefinitionByName with swc
一般 Flash 程式開發時, 取資源的方式有兩種:
.swc 擋則是 Compiler 時就會一起包進來, 要取用內部元件通常就直接
如果想要動態取得元件的話會碰到一些問題, 一般來講會用
不過編譯完執行後會碰到錯誤, 解決方式如下:
Project > Properties
Flex Compiler > 加入下面參數
- .swf
- .swc
Loader
來作處理.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var loader:Loader = new Loader(); | |
var request:URLRequest = new URLRequest('YOUR_SWF_PATH'); | |
loader = new Loader(); | |
loader.load(request); | |
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderComplete); | |
private function onLoaderComplete(event:Event):void | |
{ | |
var loader:LoaderInfo = event.currentTarget as LoaderInfo; | |
loader.removeEventListener(Event.COMPLETE, onLoaderComplete); | |
} | |
private function getClassReference(className:String):Class | |
{ | |
return loader.contentLoaderInfo.getDefinition(className) as Class; | |
} |
.swc 擋則是 Compiler 時就會一起包進來, 要取用內部元件通常就直接
new
出來就可以:var component:CLASS_NAME = new CLASS_NAME();
如果想要動態取得元件的話會碰到一些問題, 一般來講會用
getDefinitionByName(CLASS_NAME:String)
這個 method:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import flash.utils.getDefinitionByName; | |
var ClassReference:Class = getDefinitionByName("CLASS_NAME") as Class; | |
var obj:Object = new ClassReference(); |
不過編譯完執行後會碰到錯誤, 解決方式如下:
Project > Properties
Flex Compiler > 加入下面參數
-include-libraries=YOUR_ABSOLUTE_SWC_PATH.swc
留言
張貼留言