有沒有辦法從動作腳本 3 上的 .txt 檔案加載外部代碼?我想將一些 addChild() 代碼放入 .txt 檔案中,然后在 Flash 上執行,例如:
file.txt 內容
addChild(mc1);
addChild(mc2);
我的應用程式執行以下操作:
fileContents = URLRequest(file.txt);
現在,fileContents 有兩行代碼我想在 Flash 上運行,如何運行這些代碼?
謝謝
uj5u.com熱心網友回復:
您需要一個在 AS3 中動態重新創建 txt 檔案代碼的函式。
(1)將代碼行提取到一些字串陣列中。
(2)制作一個函式將每一行提取成部分。
(例如:提取命令addChild和引數mc1)。
(3)創建一個function run_Code你使用命令和引數的地方。
為簡單起見,沒有陣列的示例代碼,只是一個字串值......
var myStr = "addChild(mc1);"; //# you read this value from txt file
//# extract using length ( substr )
var myCmd = myStr.substr( 0, myStr.indexOf( "(" ) ); //extract "addChild" (eg: from start until first bracket)
//# extract using positions ( substring )
var myParam = myStr.substring( myStr.indexOf("(") 1 , myStr.indexOf(")") );
trace( "Command is: " myCmd " ... Param is: " myParam );
run_Code( myCmd, myParam );
function run_Code ( in_command:String , in_param:String ) :void
{
//# handle possible commands
if ( in_command == "addChild" ) { stage.addChild( this[ in_param ] ); }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/418271.html
標籤:
