开发者

AS3: Referencing mc's from an array to then assign further array properties

开发者 https://www.devze.com 2023-01-19 23:54 出处:网络
I have a number of \'items\' (mc\'s) contained in a scrolling mc that can be drag-dropped to other matching mc\'s.

I have a number of 'items' (mc's) contained in a scrolling mc that can be drag-dropped to other matching mc's. The items names are listed in an array and I wish to assign variables of suitability and feedback to each mc from the array also. I think this is called an associative array?

Having some trouble correctly referencing the items from the array. To explain, here's a working script with a simple array and an inefficient workaround:

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

Here's a potentially better script using an associative array, but its not working as per CAP comments...

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

Any suggestions for the AS3 newbie?



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

}


AS3 doesn't really have associative arrays, per se. What it does have is Objects, the base class of everything, which you can use just like associative arrays:

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

You can access these objects and properties a couple ways:

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

or

trace( itemArray["property1"] );


Code is not working because there are wrong types of braces. It should be new Array(), not new Array[]. See Array and Array access.

Also, you get the value of isuitable and ifeedback just like you did with iname: itemArray[i].isuitable and itemArray[i].ifeedback.


Thanks everyone for your help. Following further experimentation with some of the ideas expressed above, here is a solution that works, albeit simply at this stage:

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

精彩评论

暂无评论...
验证码 换一张
取 消