know that skspritenode has completely moved over another skspritenode

I'm trying to figure out that a SKSpriteNode has completely iterated over another SKSpriteNode, this the code I have come up so far,

if (node.frame.maxY == player.frame.minY){ 
player.physicsBody?.collisionBitMask = collisionTypes.vortex.rawValue 
}

I know it's simple enough, but I'm lost here ;(

Accepted Reply

Set the contactTestBitMask properties of both objects. then detect when they collide in the didBegin func and then detect when they stop colliding in the didEnd func


player.physicsBody?.contactTestBitMask = collisionTypes.bullet.rawValue
bullet.physicsBody?.contactTestBitMask = collisionTypes.player.rawValue

func didBegin(_ contact: SKPhysicsContact) {
  //detect your collision and do what you need to do
  //maybe set a bool to true if you have multiple collision types
}

func didEnd(_ contact: SKPhysicsContact) {
  //check that the above collison is the one that ended
  //run appropriate code for end collision
}

Replies

What do you mean by "iterated"? If I understand your subject correctly... This can be used when node 1 is just partially within the boundary of the second node:


if node1.intersects(node2) {  }


If you want to know, whether your first node is entirely within the boundaries of the second node, this should work:


// Node 1 intersects HEIGHT of Node2
if  ( node1BottomY <= node2TopY && node1TopY >= node2BottomY ) || ( node1TopY >= node2BottomY && node1BottomY <= node2TopY )
{
            intersectsHeight = true
}
// Node 1 intersects WIDTH of Node 2
if  ( node1LeftX <=node2RightX && node1RightX >= node2LeftX ) || ( node1RightX >= node2LeftX && node1LeftX <= node2RightX )
{
            intersectsWidth = true
}

// If Node1 intersects both height and width of Node 2, it is completely withoin its boundaries:
if intersectsHeight == true && intersectsWidth == true {  }


This is how you may check if half of the first node intesects the second node:


if node2.contains(CGPoint(x: node1.frame.midX, y: node1.frame.midY))

let me clarify,


node 1 is static and node 2 is dynamic, I want to know when node 2 is completely out of node 1's boundaries.

if !node2.intersects(node1)

Set the contactTestBitMask properties of both objects. then detect when they collide in the didBegin func and then detect when they stop colliding in the didEnd func


player.physicsBody?.contactTestBitMask = collisionTypes.bullet.rawValue
bullet.physicsBody?.contactTestBitMask = collisionTypes.player.rawValue

func didBegin(_ contact: SKPhysicsContact) {
  //detect your collision and do what you need to do
  //maybe set a bool to true if you have multiple collision types
}

func didEnd(_ contact: SKPhysicsContact) {
  //check that the above collison is the one that ended
  //run appropriate code for end collision
}