ActionScript3词典

时间:2013-09-15 13:32:01

标签: actionscript-3 dictionary

在AS3中,我想要一个类型为[Point] --->的关联数组。 [形状],将各种形状与空间中的点相关联。我想有这种行为:

var dict : Dictionary = new Dictionary();
var pos : Point = new Point(10, 10);
dict[pos] = new Shape();
var equalPos : Point = new Point (pos.X, pos.Y);
dict[equalPos]  // <-- returns undefined and not the shape i created before because equalPos reference is different from pos.

我希望让dict[equalPos]返回与dict[pos]相同的值,因为这些点在引用中不同,等于坐标(等于类成员)。

有什么方法可以达到这个目的吗?

2 个答案:

答案 0 :(得分:2)

更改字典的键,使用点'x和y

var key:String = point.x + "_" + point.y;//you could define a function to get key;

dict[key] = new Shape();

答案 1 :(得分:1)

我不相信你能按照你想要的方式做到这一点。

我相信你需要做的是创建一个辅助函数。 (我在尝试比较单元测试中的点时遇到同样的问题)

所以在这里,使用伪代码就是我要做的。

public static function comparePoint(point1:Point, point2:Point):Boolean{
    return (poin1.x == poin2.x && point1.y == point2.y)? true:false;
}

private function findShapeInPointDictionary(dict:Dictionary, point:Point):Shape
{
     var foundShape:Shape = null;
     for (var dictPoint:Point in dict) {
         if(comparePoint(dictPoint, point) {
       foundShape = dict[dictPoint];
         }
     }
     return foundShape;

 }
}

您的示例代码可能最终看起来像这样

var dict : Dictionary = new Dictionary();
var pos : Point = new Point(10, 10);
dict[pos] = new Shape();
var equalPos : Point = new Point (pos.X, pos.Y);
recievedShape = findShapeInPointDictionary(dict, equalPos);  
相关问题