1

I have nine SKSpriteNodes that fall down from the top to the bottom of a screen. Also i have SKView, and I can drag it over the screen. How can I detect collision of my SKView with one of the nine SKSpriteNodes dynamically ?

Igor Prusyazhnyuk
  • 133
  • 2
  • 14
  • 29

2 Answers2

2

SKView doesn't have physicsBody property, so it cannot collide.

You can, however, manually check if SKView's frame intersects with SKSpriteNode's frame:

- (void)update:(CFTimeInterval)currentTime {
    if (CGRectIntersectsRect(skView.frame, node.frame) {
        ....
    }
}
Andrey Gordeev
  • 30,606
  • 13
  • 135
  • 162
0

In the scene, you can detect collision between the frame and the SKSpriteNodes.

In your Scene.m, add the code in the initWithSize:(CGSize)size:

self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsBody.categoryBitMask = kSceneCategory;
self.physicsBody.contactTestBitMask = kSKNodeCategory; // this is your SKSpriteNode's categoryBitMask

and in - (void)didBeginContact:(SKPhysicsContact *)contact, you can detect the collision.

jseanj
  • 1,509
  • 1
  • 9
  • 9