【问题标题】:AS3 Dynamically naming object propertiesAS3 动态命名对象属性
【发布时间】:2018-04-04 23:41:10
【问题描述】:

我正在研究一种在 AS3 中的实体组件系统。在我的 EntityManager 类中,我有一个名为 createEntity 的函数。它接受一个字符串数组,然后成为实体的组件。

public function createEntity(newComponents:Array):void
    {
        var newEntity:Entity = new Entity();
        var obj:Object = new Object();
        obj.id = newEntity.id;

        // This is what I would like to do.
        var newComponentName:String;
        for (var i:int = 0; i < newComponents.length; i++)
        {
            newComponentName = newComponents[i];
            obj.newComponentName = createComponent(newComponents[i]);
        }

        trace(obj.Health); // result = undefined

        /* This was my previous setup. This way makes it really hard for my systems to access the components.
        var components:Array = [];
        for (var i:int = 0; i < newComponents.length; i++)
        {
            components.push(createComponent(newComponents[i]));
        }
        obj.components = components;
        */
    }

此外,系统通过 ID 访问实体。在 entityManager 中有一个 getEntityByID 函数。我希望能够(从 HealthSystem 类)像这样调用实体的健康组件:(简化一点)

getEntityByID(testID).Health

在使用 createEntity 函数时,所有组件都有不同的名称,所以我想动态命名它们。 如果有人对此有任何经验,将不胜感激!谢谢!

【问题讨论】:

    标签: actionscript-3 object components entity


    【解决方案1】:

    天哪。我喜欢我一直在输入我的问题,然后答案在发布后几秒钟就打动了我。

    var newComponentName:String;
            for (var i:int = 0; i < newComponents.length; i++)
            {
                newComponentName = newComponents[i];
                obj[newComponentName] = createComponent(newComponents[i]);
            }
    
            trace(obj.Health);
    

    这完全有效!不管怎么说,多谢拉。如果有人有关于这个主题或实体组件系统架构的更多信息,我仍然会很高兴。

    猜你喜欢
    • 1970-01-01
    • 2010-12-20
    • 1970-01-01
    • 2020-11-05
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多