我正在使用字典变量来匹配drop和drag exercice。
var dict = new Dictionary ();
dict[box_a]=s1;
dict[box_b]=s2;
dict[box_c]=s3;
dict[box_d]=s4;
问题1:最后我想检查box_a ==是否与s1等等......我该怎么做
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;
问题2:我希望box_d也接受s3我该怎么做 如果我做 DICT [box_d] = S4;和dict [box_d] = s3;它不会工作,因为它与最后一个相等。 :( 谢谢!!
答案 0 :(得分:5)
Dictionary是键/值的集合,就像一个表:
+------+--------+
| key1 | value1 |
+------+--------+
| key2 | value2 |
+------+--------+
| key3 | value3 |
+------+--------+
| key4 | value4 |
+------+--------+
要访问这些值,您必须使用密钥,即:dict[key2]
返回value2
您无法直接访问密钥,购买时可以使用for ()
语句访问密钥,因此:
for (var key:* in dict) {
trace('dict[' + key + '] = ' + dict[key]);
}
将输出:
dict[key1] = value1
dict[key2] = value2
dict[key3] = value3
dict[key4] = value4
您正在使用for each ()
声明。使用该特定语句,您将获得另一个结果,因为您正在遍历值而不是键,因此:
for each (var value:* in dict) {
trace(value);
}
将输出:
value1
value2
value3
value4
因此,如果您想使用密钥检查某些内容,则必须使用for (key in dict)
表单。
我建议你使用这样的数组:
dict[box_a] = [s1];
dict[box_b] = [s2];
dict[box_c] = [s3];
dict[box_d] = [s3, s4];
然后将值作为数组访问并检查所有值:
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
}
}
答案 1 :(得分:0)
似乎只能检查基本类型的等式,用==
替换===
运算符不会改变任何内容:
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);
输出
[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
但是,obj == obj
会返回true
;)