Dusty
Dusty

Reputation: 129

Dictionary () as3

I am using a dictionary variable to match up a drop and drag exercice.

var dict = new Dictionary ();
dict[box_a]=s1;
dict[box_b]=s2;
dict[box_c]=s3;
dict[box_d]=s4;

question 1: at the end i would like to check if box_a== with s1 and so on .... how would I do that

for each( var item  in dict)
    {   item.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
        item.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
    if(item==????)
// what do i have to put instead of ???
    hits=hits+1;

question 2:I would like box_d to accept also s3 how would i do that if i do dict[box_d]=s4; and dict[box_d]=s3; it wont work because it matches it with the last equal. :( Thanks !!

Upvotes: 0

Views: 6738

Answers (2)

Intro

A Dictionary is a collection of key/value, is like a table:

+------+--------+
| key1 | value1 |
+------+--------+
| key2 | value2 |
+------+--------+
| key3 | value3 |
+------+--------+
| key4 | value4 |
+------+--------+

To access the values you must use the keys i.e: dict[key2] returns value2

Answer 1:

You can't access the keys directly, buy you can use a for () statement to access them, so this:

for (var key:* in dict) {
    trace('dict[' + key + '] = ' + dict[key]);
}

will output this:

dict[key1] = value1
dict[key2] = value2
dict[key3] = value3
dict[key4] = value4

You are using a for each () statement. With that particular statement you have another result, because you are iterating through the values and not the keys, so this:

for each (var value:* in dict) {
    trace(value);
}

will output this:

value1
value2
value3
value4

So if you want to check something with a key you must use the for (key in dict) form.

Answer 2:

I recommend you to use Arrays like this:

dict[box_a] = [s1];
dict[box_b] = [s2];
dict[box_c] = [s3];
dict[box_d] = [s3, s4];

and then access the values as arrays and check with all of the values:

for (var key:* in dict) {
    var values:Array = dict[key];
    for each (var value:* in values) {
        // do what you want with s1, s2, s3, etc
    }
}

Upvotes: 5

www0z0k
www0z0k

Reputation: 4434

it seems like equality can be checked only for primitive types, replacing == operator with === changes nothing:

        var dict:Dictionary = new Dictionary();
        dict['ololo1'] = { one : 1, two : '2' };
        dict[ { one : 1, two : '2' } ] = function(o:*):String { return o as String; };
        dict[function(o:*):String { return o as String; }] = 'ololo';
        dict['ololo2'] = 'ololo2';
        dict[{ one : 1, two : '2' }] = { one : 1, two : '2' };
        for (var key:* in dict) {
            trace(key, dict[key], key == dict[key]); 
        }
        trace('equal functions :', (function(o:*):String { return o as String; } === 
                                    function(o:*):String { return o as String; } ));
        trace('equal objects :', ({ one : 1, two : '2' } === 
                                  { one : 1, two : '2' } ));
        trace('equal functions :', (function(o:*):String { return o as String; } == 
                                    function(o:*):String { return o as String; } ));
        trace('equal objects :', ({ one : 1, two : '2' } == 
                                  { one : 1, two : '2' } ));

        var obj:Object = { one : 1, two : '2' };
        var obj1:Object = { one : 1, two : '2' };
        var fun:Function = function(o:*):String { return o as String; };
        var fun1:Function = function(o:*):String { return o as String; };
        trace(obj == { one : 1, two : '2' }, 
              obj == obj1,
              fun == function(o:*):String { return o as String; },
              fun == fun1);

outputs

[object Object] [object Object] false
function Function() {} ololo false
[object Object] function Function() {} false
ololo2 ololo2 true
ololo1 [object Object] false
equal functions : false
equal objects : false
equal functions : false
equal objects : false
false false false false

however, obj == obj returns true ;)

Upvotes: 0

Related Questions