AS3 - 使用另一个变量字符串值的目标变量

时间:2013-02-07 13:28:27

标签: string actionscript-3 variables target

好。所以我有一个功能,我通过它传递了一个名为 objectName 的参数。 不,我不知道怎么做或解释它。所以这里。

public function moveObject(ObjectName):void{


           //objectName now holds "myName" which is an object also

           //i would now like my variable called myNamePosition to equal 10

           //so it would need to grab the value of objectName which is myName:Object.
           //turn it into a string of some kind - myName:string
           //add "Position" to the end of it so its myNamePosition
           // make it equal to 10

           trace(myNamePosition);

    }

传递的函数参数会发生变化,所以我实际上无法使用" myName"。而是" objectName"。

由于

1 个答案:

答案 0 :(得分:1)

示例:

package
{
import flash.display.MovieClip;


public class astest extends MovieClip
{
    public function astest()
    {
        init();
    }

    private var myNamePosition:int;
    private function init():void
    {
        moveObject({myName:{}})
    }

    public function moveObject(objectName:Object):void
    {
        var propName:String;
        for(propName in objectName)
            break;
        trace(propName);

        propName+="Position";
        trace(propName);

        this[propName] = 10;

        var propValue:* = this[propName];
        trace(propValue);
    }   
}
}

输出:

myName
myNamePosition
10

这是你需要的吗?