AS3:从数组引用mc然后分配更多的数组属性

时间:2010-10-15 16:27:41

标签: flash actionscript-3

我有一些滚动的mc中包含的“项目”(mc),可以拖放到其他匹配的mc中。 项目名称列在数组中,我希望也为数组中的每个mc分配适用性和反馈的变量。我认为这被称为关联数组?

在正确引用数组中的项目时遇到一些问题。 为了解释,这是一个带有简单数组和低效解决方法的工作脚本:

var itemArray:Array = new Array("ball","box","hex");  //only a few items in this prototype
scrollitems.ball.ifeedback = "Woo... hoo...";
scrollitems.box.ifeedback = "Great!";
scrollitems.hex.ifeedback = "Oops!";
scrollitems.ball.isuitable = true;
scrollitems.box.isuitable = true;
scrollitems.hex.isuitable = false;

for (var i:int=0; i<itemArray.length; i++)
{
   var itemname:String = String(itemArray[i]);
   var curritem:MovieClip = MovieClip(scrollitems.getChildByName(itemname));
   if (curritem != null)
   {
     curritem.startX = curritem.x;
     curritem.startY = curritem.y;
     curritem.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
     curritem.addEventListener(MouseEvent.MOUSE_UP, dropIt);
     curritem.buttonMode = true;
   }
}

这是一个使用关联数组的可能更好的脚本,但它不能按照CAP注释......

工作
var itemArray:Array = new Array[{iname:"ball",isuitable:true,ifeedback:"Well done!"},
     {iname:"box",isuitable:true,ifeedback:"Great!"},
     {iname:"hex",isuitable:false,ifeedback:"Oops!"}];
for (var i:int=0; i<itemArray.length; i++)
{
   var itemname:String = String(itemArray[i].iname); // THIS DOESNT WORK - ITEMNAME IS A STRING BUT CANT ASSIGN INAME TO THIS STRING??
   var curritem:MovieClip = MovieClip(scrollitems.getChildByName(itemname));
   if (curritem != null)
   {
     curritem.startX = curritem.x;
     curritem.startY = curritem.y;
     curritem.isuitable= curritem.isuitable;  //NOT WORKING - HOW TO ASSIGN THIS??
     curritem.ifeedback = curritem.ifeedback;  // NOT WORKING - HOW TO ASSIGN THIS??
     curritem.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
     curritem.addEventListener(MouseEvent.MOUSE_UP, dropIt);
     curritem.buttonMode = true;
   }
}

对AS3新手的任何建议?

4 个答案:

答案 0 :(得分:0)

AS3本身并没有真正的关联数组。它所拥有的是Objects,它是所有东西的基类,你可以像关联数组一样使用它们:

var itemArray:Object = { 
    property1:"value", 
    property2:{ 
        subProperty1:"subValue", 
        subProperty2:{ //more nested objects ad infinitum }
    } 
};

您可以通过以下几种方式访问​​这些对象和属性:

trace( itemArray.property1 );
trace( itemArray.property2.subProperty1 );

trace( itemArray["property1"] );

答案 1 :(得分:0)

代码无效,因为大括号错误。它应该是new Array () ,而不是new Array[]。请参阅ArrayArray access

此外,您获得isuitableifeedback的价值,就像使用inameitemArray[i].isuitableitemArray[i].ifeedback一样。

答案 2 :(得分:0)


var itemArray:Array = [{iname:"ball",isuitable:true,ifeedback:"Well done!"},
     {iname:"box",isuitable:true,ifeedback:"Great!"},
     {iname:"hex",isuitable:false,ifeedback:"Oops!"}];
 for (var i:int=0; i < itemArray.length; i++)
 {
     var itemname:String = itemArray[i].iname; 
     var curritem:MovieClip = MovieClip(scrollitems.getChildByName(itemname));

     if (curritem != null)
     {
       curritem.startX = curritem.x;
       curritem.startY = curritem.y;
       curritem.isuitable= itemArray[i].isuitable;
       curritem.ifeedback = itemArray[i].ifeedback;

       curritem.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
       curritem.addEventListener(MouseEvent.MOUSE_UP, dropIt);
       curritem.buttonMode = true;
     }

}

答案 3 :(得分:0)

感谢大家的帮助。 在进一步尝试上面提到的一些想法后,这里有一个有效的解决方案,尽管只是在这个阶段:

var itemArray:Object = {  
item1:{iname:"ball", isuitable:true,  ifeedback:"Great!"},
item2:{iname:"box",  isuitable:false, ifeedback:"No, not that one!"}
} 

for (var i:int = 1;i < 3;i++) {
    var itemname:String = String(itemArray["item"+i].iname);
    var curritem:MovieClip = MovieClip(scrollitems.getChildByName(itemname));

    if (curritem != null)
    {
       curritem.isuitable = itemArray["item"+i].isuitable;
       curritem.ifeedback = itemArray["item"+i].ifeedback;
       curritem.startX = curritem.x;
       curritem.startY = curritem.y;

       curritem.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
       curritem.addEventListener(MouseEvent.MOUSE_UP, dropIt);
       curritem.buttonMode = true;
    }
}