Reputation: 1265
I'm building somewhat of a really awful platform game and I'm trying to detect if parts of a node are in the same location as parts of another node. I'll use method:
func intersectingNode(node: SKNode) -> SKNode?{
for floorObject in floorObjects{ // floorObjects is of type [SKNode]
if floorObject.intersectsNode(node){
return floorObject
}
}
return nil
}
I should point out at this time that the anchor points of all nodes involved here are at (0.5, 0.5). The anchor point of the main SKScene is at the bottom left corner of the window.
This method is supposed to return the object in the floorObjects
array that node
is intersecting with, and if it's not intersecting with any object in the floorObjects
array, the method will return no node. But my problem is this: the method is not returning any node if the x position of node
is less than the x position of floorObject
and the y position of node
is greater than the y position of floorObject
. Now since the anchor point on all nodes are at (0.5, 0.5), some of that area is on top of the top-left quarter of floorObject
.
If I change the third line to this:
if node.intersectsNode(floorObject){
the x position of floorObject
would be less than the x position of node
and the y position of floorObject
would be greater than the y position of node
.
So how can I make sure that the method above is always returning a node if anywhere in the frame of one node is somewhere inside of the frame of another node?
Upvotes: 1
Views: 899
Reputation: 794
You can compare the frame
s directly, which ought to give you what you need:
if node.frame.intersects(floorObject.frame) {
return floorObject
}
Upvotes: 1