外部スクリプトファイルの読み込み
mx:Script@source、またはincludeステートメントで、外部のスクリプトファイルを読み込みます。
mx:Script@sourceを使う。
mx:Scriptのsource属性で指定した外部スクリプトファイルを読み込み、その中のmain関数を実行します。
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete = "main()" > <!-- ラベルを貼り付ける --> <mx:Panel title="" width="200" height="100"> <mx:Label id="stdout" /> </mx:Panel> <!-- 関数の定義を外部ファイルから読み込み。 外部ファイルのパスを相対パスで指定する。 --> <mx:Script source="functions.as"/> </mx:Application>
外部スクリプト(functions.as):
public function main():void { stdout.text = "Hello World!!"; }
functions.asは相対パスで指定します。今回はMXMLと同じディレクトリに配置しています。
ファイル構成:
+root -include_sample.mxml -include_sample-config.xml -functions.as
includeステートメントを使う。
includeステートメントを使用する場合、次のようになります。
MXML:
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete = "main()" > <!-- ラベルを貼り付ける --> <mx:Panel title="" width="200" height="100"> <mx:Label id="stdout" /> </mx:Panel> <mx:Script><![CDATA[ // クラスファイルを外部ファイルから読み込む。 include "./functions.as"; ]]></mx:Script> </mx:Application>