How to use SKNode's isEqual(to:) function? confusing.

Seeing the explanation "Compares the parameter node to the receiving node." and the name of function, it sounds like " comparing the receiver with the parameter node".

However, "

true
if the node is a descendant of the
parent
node; otherwise
false
." down the description gives me different thought about it that it exists to check if the parameter node is a child node of the receiver.


I couldn't get it right just by seeing the explanation: it is not clear.


Here it goes the link I'm refer to

https://developer.apple.com/documentation/spritekit/sknode/1483078-isequal


I'm very confused at the description isEqual(to node: SKNode) -> Bool

Do anyone give me a clear explanation about this?


Thanks in advance.

Replies

The SKNode header file states that isEqual(to:) "Returns true if this node has equivalent content to the other object, otherwise false".


Here is an example:


let node1 = SKNode()

self.addChild(node1)

node1.name = "node"


let node2 = SKNode()

node2.name = "node"


let node3 = SKNode()

node3.name = "differentNode"

self.addChild(node3)

print(node1.isEqual(to: node2)) // true (even though the nodes have different parents, they have the same content, so this is true)

print(node1.isEqual(to: node3)) // false (even though the nodes have the same parents, they have different content, so this is false)

print(node2.isEqual(to: node3)) // false (the nodes have different parents and different content, so this is false)


As you can see from this example, the content of the node is the important differentiator when using isEqual(to:).

Thanks for the reply. You saved my effort a lot!


However, your answer gave me another question as the followings:


From the description

https://developer.apple.com/documentation/spritekit/sknode/1483078-isequal

has

"

true
if the node is a descendant of the
parent
node; otherwise
false
."


It seems that it also care about the parent node.


But you said it the function only cares about the content.


Then does the description itself has the its own flaw? If so, please bug report to Apple.


Unless I do the experimental coding, it would be difficult to know what's going on, because Apple's description give rise to confusion.



Thanks in advance.