Search code examples
swiftsprite-kitpositionnodesframes

One frame contains another frame or part of it SpriteKit swift iOS


I am developing my first game with SpriteKit and have the following problem: I need to detect if one frame contains another frame or at least a part of it. Typical function is node.contains(anotherNode.position). But I need to check not one CGPoint(position) but the whole frames. Could somebody help?


Solution

  • Given 2 sprites

    let spriteA = SKSpriteNode()
    let spriteB = SKSpriteNode()
    

    you can check if spriteA is inside spriteB

    spriteA.frame.contains(spriteB.frame)
    

    Please note that this will not work with simple SKNode(s), you actually need a subtype which has a frame like SKSpriteNode, SKLabelNode, ...