Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.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
Flash AS3:引用数组中的mc,然后分配进一步的数组属性_Flash_Actionscript 3 - Fatal编程技术网

Flash AS3:引用数组中的mc,然后分配进一步的数组属性

Flash AS3:引用数组中的mc,然后分配进一步的数组属性,flash,actionscript-3,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... h

我有一个滚动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新手有什么建议吗?

AS3本身并没有关联数组。它所具有的是对象,即所有对象的基类,可以像关联数组一样使用:

var itemArray:Object = { 
    property1:"value", 
    property2:{ 
        subProperty1:"subValue", 
        subProperty2:{ //more nested objects ad infinitum }
    } 
};
可以通过以下几种方式访问这些对象和属性:

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


代码不起作用,因为大括号的类型错误。它应该是新数组,而不是新数组[]。见和

此外,您还可以获得isuitable和ifeedback的值,就像对iname所做的那样:itemArray[i]。isuitable和itemArray[i]。ifeedback

}
谢谢大家的帮助。 在对上述一些想法进行进一步实验后,这里有一个可行的解决方案,尽管只是在现阶段:

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;
    }
}

实际上,您可以在as3中创建关联数组,但不建议这样做,因为数组类方法对数组不可用:livedoc页面告诉我们,尽管ActionScript允许您使用数组类创建关联数组,但您不能将任何数组类方法或属性与关联数组一起使用。-虽然我意识到这不是一个答案,但我觉得这一点很重要,值得一提。。当您使用数组作为关联数组时,您必须创建自己的函数来计算数组的长度,这样可以更好地避免这种情况。感谢您的这些想法。对于我的问题,所有建议都指向创建关联数组,但使用livedoc建议的对象类。我希望mc实例名称是iSuite和ifeedback的其他属性在名称中的键。所以这就是理论,在实施过程中仍然遇到麻烦…对使用字典有什么想法吗??如果你需要复杂的对象作为键,我在某个地方读到了使用它的方法——不确定这是否是我这里的方法。感谢数组访问技巧——我仍然在想什么时候使用{}、[]或。虽然我在这个场合的用法似乎是正确的。关于第二点,您将从我的第二个代码段中的CAPS注释中注意到,我无法获得iname的值,因此也无法使用此技术获取ifeedback和isuitable值。谢谢Patrick,这与数组一样有效。我还在下面发布了一个使用对象列表的解决方案。 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;
     }
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;
    }
}