views:

72

answers:

4

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?

A: 

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"] );
Logic Artist
You actually can do associative arrays in as3, it is not recommended since the array class methods are not available to the array: a livedoc page tells us "Although ActionScript permits you to create associative arrays using the Array class, you cannot use any of the Array class methods or properties with associative arrays." - http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/Array.html While I realize this is not an answer, I felt it is important enough to mention.
exoboy
exoboy is right.. when you use array as associative array , you will have to make ur own function to even count the length of the array so better avoided.
Muhammad Irfan
I stand corrected. :)
Logic Artist
Thanks for those ideas. For my problem all the advice points to creating an associative array, but use the Object class as the livedoc recommends. I want the mc instance name to be the key (iname) with other properties for isuitable and ifeedback. So thats the theory, still getting into trouble with the implementation...
ss
Any thoughts on using Dictionary?? I read somewhere to use it if you need complex objects as keys -- not sure if thats what I have here.
ss
A: 

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.

Miha
Thanks for the array access tips - still getting my head around when to use {}, [], or (). Although my usage appears correct on this occasion. On your second point, you'll note from my CAPS comments in my 2nd code snippet that I could not get the value of iname to work, so cannot use this technique to get ifeedback and isuitable values either.
ss
A: 

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

}

PatrickS
Thanks Patrick, that works well as an Array.I've also posted a solution below that uses a list of Objects.
ss
A: 

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