Malcolm
Malcolm

Reputation: 49

How to Detect if a SKSpriteNode is in a SKShapeNode

I put a circular attack oval on a blue space ship and, if any SKSpriteNode enters the SKShapeNode (attack oval) the blue space ship can fire on the SKSpriteNode. The problem is that I can't figure out how to do the detection. I have tried physical bodies but, I can't have contact without having a collision.

Here is an example. I want the purple/blue attack circle to detect A,B,C,D,E

I want the purple circle to detect A,B,C,D,E

Here is the code I think you will need. RSSprite is short for red ship sprite

blueShip.position = CGPoint(x: 800, y: 400)

attackCircle = SKShapeNode(ellipseOfSize: CGSize(width: 1000, height: 400))
attackCircle.position = CGPoint(x: blueShip.position.x, y: blueShip.position.y)

RSSprite!.position = CGPoint(x: 200, y: 700)
RSSprite!.physicsBody = SKPhysicsBody(rectangleOfSize: RSSprite!.size)

Upvotes: 2

Views: 365

Answers (1)

sangony
sangony

Reputation: 11696

In the update method you can check for an intersection of 2 nodes like this:

if([nodeA intersectsNode:nodeB]) {
    // you have a collision
}

Upvotes: 2

Related Questions