从字符串执行as3代码 [英] executing as3 code from a string

查看:208
本文介绍了从字符串执行as3代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个简单的文本rpg,并将所有数据对象存储为xml文件,但是我需要能够对许多事情运行一些简单的语句.

I'm working on a simple text rpg and I'm storing all of my data objects out as xml files but I need to be able to run some simple statements for many things.

我做了一些谷歌搜索,但还没有提出很多建议.

I have done some googling I havent come up with much.

我想做的是采用简单的语句,例如:

What I'm trying to do is take simple statements like:

playerhp += 15;

if(playerisvampire == 1) {blah blah;}

并将它们嵌入xml结构中,以便项目或对话行可以包含检查和可执行代码,而rpg类则更多地用作解释器和接口.这样的事情有可能吗?

and embed them inside of the xml structure so that an item or conversation line can contain the checks and executable code leaving the rpg class as more of an interpreter and interface. Is such a thing possible?

推荐答案

ActionScript 3不再包含eval函数,因此无法直接实现.但是,您可以滚动自己的简单解释器来手动执行此操作.像这样:

ActionScript 3 contains no eval function anymore, so this is not possible directly. However, you can roll your own simple interpreter to do this manually. Something like this:

var item:XML =
    <health_item>
        <action name="hp_change" value="15"/>
    </health_item>;

在ActionScript中检查动作名称,找到相应的函数并使用"value"参数调用它:

Check action name in ActionScript, find corresponding function and call it with "value" argument:

for each (var action:XML in item.action) {
    var actionName:String = action.@name;

    //switch variant
    switch (actionName) {
        case "hp_change":
            hpChange(action.@value);
            break;
        //and so on for other known actions
    }

    //direct call by name variant
    if (hasOwnProperty(actionName)) {
        this[actionName](action.@value);
    } else {
         //report error
    }
}

这篇关于从字符串执行as3代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆