2

AS3 では、さまざまな形状を空間内の点に関連付ける [点] ---> [形状] 型の連想配列が必要です。私はこの振る舞いをしたいと思います:

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]

これを達成する方法はありますか?

4

2 に答える 2

2

辞書のキーを変更し、ポイントの x と y を使用します

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

dict[key] = new Shape();
于 2013-09-15T14:48:10.680 に答える
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);  
于 2013-09-15T13:52:47.157 に答える