【问题标题】:在 Action Script 3.0 上从 TXT 文件加载代码
【发布时间】:2021-03-10 12:00:03
【问题描述】:

有没有办法从动作脚本 3 的 .txt 文件中加载外部代码?我想将一些 addChild() 代码放在一个 .txt 文件中,然后在 Flash 上执行,例如:

file.txt 内容

addChild(mc1);
addChild(mc2);

我的应用程序执行以下操作:

fileContents = URLRequest(file.txt);

现在,fileContents 有两行代码我想在 Flash 上运行,如何运行这些代码?

谢谢

【问题讨论】:

  • 根据stackoverflow.com/questions/5513048/…,您无法将字符串评估为可运行代码。您必须创建自己的解释器
  • SWF 应用程序包含源 ActionScript3 的编译版本(所谓的 ABC-code),所以不,它是无法在您的应用程序中运行其他未编译的 ActionScript3 指令。如果您认为这正是您想要的,那么您最好的选择(我认为)是使用某种(XML- 或 JSON-based)模拟语言一个简单的解释器来执行它的指令。
  • 感谢您的回答。是否有机会至少让 addChild 代码识别 .txt 文件中的内容?示例:addChild(fileContents) ?
  • addChild 仅指DisplayObject 实例和子实例,而不是来自fileContents 的原始字符串。如果你想在屏幕上显示文本,你首先必须创建一个TextField(假设它被命名为contentTextField),然后使用addChild(contentTextField)
  • 不,我更多的是谈论带有我要添加的影片剪辑名称的 .txt 文件,所以,addChild(fileContents),如果我将 mc1 放在 .txt 文件中,我的 Flash将在我的库中添加影片剪辑 mc1。我记得在 AS 2 中,使用 MovieClip["customName"] 这样的东西是可能的。

标签: actionscript-3 flash actionscript


【解决方案1】:

您需要一个在 AS3 中动态重新创建 txt 文件代码的函数。

(1) 将代码行提取到某个字符串数组中。

(2) 制作一个函数将每一行提取成部分。
eg: 提取命令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 ] ); }
    
}

【讨论】:

  • 谢谢伙计,这无疑对我有很大帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多