AS2 - 如何拆分文本字符串以生成函数和变量

时间:2012-10-22 11:30:14

标签: flash actionscript actionscript-2

好的,所以我有一个调试系统,我正在为一个Flash游戏工作,我有一个Switch(case)块在这里是我到目前为止。

switch (fstring){

case "player.getname":
var pname = _global.heroVars.playerName;
target.feedback.text +=  (ct + pname + "\r\n");
target.inputt.text = "";
break;

case "player.getlevel":
var plevel = _global.heroVars.userLevel;
target.feedback.text += (ct + plevel + "\r\n");
target.inputt.text = "";
break;

default:
target.feedback.text += (ct + "\"" + inp + "\"" + " is not a valid function\r\n");
target.inputt.text = "";
}

我的问题是如何制作一个这样的“Set”功能。例如
“player.setlevel NUMBERHERE”或“player.setname STRINGHERE”等。并取字符串的第二部分并使用所述数字或字符串来设置变量?

1 个答案:

答案 0 :(得分:0)

了解如何做到这一点。

以字符串“ _player.addlevels 2 ”为例,var inputFunc

var mystring:String = inputFunc; // sets it to a new var to not mess up the original
var sepString:Array = mystring.split(" "); // splits "_player.addlevels" and "2"
var func:String = sepString[0]; // sets this string to "_player.addlevels"
var inputVar:String = sepString[1]; // sets this string to "2" which can be anything and cast into another var type

switch(func){ // takes our variable with the string "_player.addlevels"
    case "_player.addlevels":
        _global.playerLevel += int(inputVar); //since they are using this function we know that they are using and integer and thus cast the string to an int
    break;
}

不知道我以前怎么没看到,但我最终解决了自己的问题。所以,如果有人好奇,那就是答案