1

I was wondering why to equal points cannot be compared and shown as equal using '=='

e.g

var p1:Point = new Point( 1, 5 );
var p2:Point = new Point( 1, 5 );

trace( p1 == p2 )                   //false
trace( p1.x == p2.x, p1.y == p2.y ) //true true
trace( p1.equals( p2 ))             //true

It seems weird and a little pointless (pun)
Could anybody shed some light on why this is?

4

1 に答える 1

7

p1 == p2 compares the two objects and not the x and y components of the objects. Since p1 and p2 are different objects(created by new Point) p1 == p2 returns false.

The .equals() method does a comparison of the x and y components so it returns true.

The following would return true :

var p1:Point = new Point(1,5);
var p2:Point = p1;
trace(p1==p2);

Because p1 and p2 are actually the same object.

于 2012-10-04T00:42:49.060 に答える