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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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
Actionscript 3 将对象拥有的变量传递给函数_Actionscript 3_Variables - Fatal编程技术网

Actionscript 3 将对象拥有的变量传递给函数

Actionscript 3 将对象拥有的变量传递给函数,actionscript-3,variables,Actionscript 3,Variables,如果我将“\u atr”替换为“\u person.strength”(两者都没有引号),代码工作正常,但其他情况下它不会更改“person.strength”。当您将\u atr作为\u person.strength传递给函数时,\u person.strength仅作为一个值。 将(对象)\u person和\u atr作为字符串传递 function getAttribute(_person, _atr):void { _atr = getNumber(0, 10); // Here i

如果我将“\u atr”替换为“\u person.strength”(两者都没有引号),代码工作正常,但其他情况下它不会更改“person.strength”。当您将
\u atr
作为
\u person.strength
传递给函数时,
\u person.strength
仅作为一个值。 将(对象)
\u person
\u atr
作为字符串传递

function getAttribute(_person, _atr):void
{
_atr = getNumber(0, 10); // Here is the problem
_person.points -= _atr;
}

您的函数还有第二个参数,
\u atr
,您可以将其传递给
\u person.strength

但是,在这个函数中,您要做的第一件事就是将
\u atr
的值更改为其他值。 向
\u atr
参数传递任何内容都没有意义,因为它无论如何都会被忽略


如果您指定了所有变量、函数参数和函数返回值的类型,您会有很大的帮助。

您到底想做什么?什么是getNumber()?谢谢。这解决了我的问题。因此,将变量作为字符串传递,然后使用[]将其分配给该对象。:)我不知道。你是对的,但那不是我的问题。我的问题是,atr根本不会被认为是个人的var。下面的答案解决了这个问题。谢谢。@randyHurd我还是想知道你想要完成什么,依靠字符串来识别属性似乎是个(非常)坏的主意。“人”有点、力量、速度、敏捷等等。。。我需要“滚动”一个10面骰子(基本上)并扣除这些分数,然后将它们添加到每个属性中。我需要为每个属性的每个“人”这样做。而不是在滚动后为每个属性使用一行代码。。。e、 g.人的力量=getNumber(0,10);那么person.points-=person.strength等…为什么这是一个非常糟糕的主意?我对我知道的代码很在行,但有很多我不知道。我确实想高效、正确地做事。
function getAttribute(_person, _atr):void
{
_atr = getNumber(0, 10); // Here is the problem
_person.points -= _atr;
}
function getAttribute(_person, _atr):void
{
    _person[_atr] = getNumber(0, 10); 
    // _person[_atr] is _person.strength if _atr is "strength".
    _person.points -= _person[_atr];
}