for循环(循环变量名称)

时间:2011-10-31 17:20:09

标签: flash actionscript-3

我有一些变量a1到a14和另一组变量q1到q14,我试图像这样做一个for循环

for(var i:uint = 1; i < 15; i++)
{ 
 if (this("a"+i).x== this("q"+i).x )
{points= points+1 }
else
{
this("q"+i.visible=false;
// shows the good answer 
 }
} 

请帮助:)

1 个答案:

答案 0 :(得分:1)

对象的属性可以通过方括号访问,类似于数组中的索引。 每个属性名称(变量名称)实际上是一个变量键。 还建议使用Object.hasOwnProptery(propertyName:String)方法检查对象实际上是否具有该属性。

http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00001424.html

示例:

 for(var i:uint = 1; i < 15; i++){ 
    if (this["a"+i].x== this["q"+i].x ){
        points= points+1;
    }
    else{
        this["q"+i].visible=false;
        // shows the good answer 
    }
} 

我没有检查过,但是在创建变量名时使用Number而不是uint和toString来检索它的字符串值可能是明智的

相关问题