Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String Actionscript 3-获取一个变量';按字符串名称指定的值_String_Actionscript 3_Var - Fatal编程技术网

String Actionscript 3-获取一个变量';按字符串名称指定的值

String Actionscript 3-获取一个变量';按字符串名称指定的值,string,actionscript-3,var,String,Actionscript 3,Var,我发现了一个与此类似的问题,但给出的答案在我的情况下不起作用。 让我解释一下: 我正在做一个视觉小说风格的文本框作家。基本功能如下:从dynamic texfield“dialogueText”中读取文本,将其存储为字符串,清除文本框并为字符添加回存储的字符串。它工作得很好,但我希望该功能能够用括号中指定的变量值替换双括号之间的文本,例如((playerName))。将括号之间的内容保存为字符串也可以,但是我还必须找到一种方法来打印括号之间指定的变量的值 例如: var playerName:S

我发现了一个与此类似的问题,但给出的答案在我的情况下不起作用。
让我解释一下:

我正在做一个视觉小说风格的文本框作家。基本功能如下:从dynamic texfield“dialogueText”中读取文本,将其存储为字符串,清除文本框并为字符添加回存储的字符串。它工作得很好,但我希望该功能能够用括号中指定的变量值替换双括号之间的文本,例如((playerName))。将括号之间的内容保存为字符串也可以,但是我还必须找到一种方法来打印括号之间指定的变量的值

例如:

var playerName:String = "Anon";

function playFrame(frame:String):Function {
   return function(evt:Event = null):void{
      ...
      dialogueLine = dialogueText.text;
      for(var i:int = 0; i < dialogueLine.split("((").length - 1; i++){
         var dialogueVar:String = dialogueLine.substring(dialogueLine.indexOf("((") + 2, dialogueLine.indexOf("))"));
         //dialogueVar now contains whatever variable name is specified between (( ))   
         dialogueLine = dialogueLine.split("((" + dialogueVar + "))").join(***);
         //In join, *** has to be something that uses dialogueVar to look up a variable with name 'dialogueVar' (e.g. playerName), and then give it's value (which in this case would be 'Anon')
      }
      dialogueText.text = "";
      ...
   }
}
var playerName:String=“Anon”;
函数playFrame(帧:字符串):函数{
返回函数(evt:Event=null):void{
...
dialogueLine=dialogueText.text;
对于(var i:int=0;i
有人建议我对此使用[dialogueVar],但在使用trace()进行测试时,我得到了“未定义”。是否有人有一个选项不为每个可能的变量硬编码switch语句

我尝试了
这个[dialogueVar]
…但是失败了 使用
trace()

如果你的代码在一个类中(例如:不是写在Actions面板上),那么简单的解决方法是首先声明变量(作为公共变量或私有变量),然后在其他函数中使用它们而不会出错

//# declare public variables (before using inside any later functions)
public var playerName :String = "";
public var dialogueLine :String ="";
我希望该功能能够用括号中指定的变量值替换双括号之间的文本,例如
((playerName))

使用上述修复程序,您自己的代码尝试应该可以工作。测试
join
如下:

dialogueLine = dialogueLine.split("((" + dialogueVar + "))").join( this[dialogueVar] + "...!!");
//# declare public variables (before using inside any functions)
public var playerName :String = "";
public var dialogueLine :String ="";

//# inside some fuction, update playerName with expected "Anon"...
playerName  = "Anon";

//# your problematic function now works like this...
function playFrame(frame:String):Function 
{
    return function(evt:Event = null):void
    {
        ...
        dialogueLine = dialogueText.text;
        //dialogueLine = "hello there, I see your name is ((playerName))";

        for(var i:int = 0; i < dialogueLine.split("((").length - 1; i++)
        {
            var dialogueVar:String = dialogueLine.substring(dialogueLine.indexOf("((") + 2, dialogueLine.indexOf("))"));
            dialogueLine = dialogueLine.split("((" + dialogueVar + "))").join( this[dialogueVar] + "...!!");

        }
        dialogueText.text = "";
        ...
    }
}
请记住,使用
dialogueVar=
获得的任何值都必须存在一个公共或私有变量

最后,您的示例代码应该如下所示:

dialogueLine = dialogueLine.split("((" + dialogueVar + "))").join( this[dialogueVar] + "...!!");
//# declare public variables (before using inside any functions)
public var playerName :String = "";
public var dialogueLine :String ="";

//# inside some fuction, update playerName with expected "Anon"...
playerName  = "Anon";

//# your problematic function now works like this...
function playFrame(frame:String):Function 
{
    return function(evt:Event = null):void
    {
        ...
        dialogueLine = dialogueText.text;
        //dialogueLine = "hello there, I see your name is ((playerName))";

        for(var i:int = 0; i < dialogueLine.split("((").length - 1; i++)
        {
            var dialogueVar:String = dialogueLine.substring(dialogueLine.indexOf("((") + 2, dialogueLine.indexOf("))"));
            dialogueLine = dialogueLine.split("((" + dialogueVar + "))").join( this[dialogueVar] + "...!!");

        }
        dialogueText.text = "";
        ...
    }
}

我强烈反对使用闭包(可以在其他函数中创建未命名的未绑定函数)原因有几个。其中一个,在闭包中使用的这个引用,指向谁知道确切的位置,因为闭包没有绑定到任何特定的对象。然后,您需要一个对象,它是您想要的所有变量的集合,比如var sessionVars:object={playerName:“Anon”,playerGender:“F”};然后得到一个变量var aValue:String=sessionVars[variableName];我使用闭包的原因是因为我必须能够使用特定参数调用playFrame,即“next”、“prev”和要跳转到的帧标签的名称。因为据我所知,这是唯一有效的方法。不过,你建议使用对象对变量进行分组似乎很有希望,我会尝试一下!这是可行的,但问题是在双括号之间的任何变量都必须是其他变量,比如playerAge或其他任何变量。这样,我必须为我想要使用的每个变量创建一个巨大的switch语句,这就是我试图避免的,我现在更好地理解了你的问题。我更新了答案并添加了一个PS:以防万一r代码不接受
public
private
变量。