Search code examples
swiftsprite-kitskspritenode

How to detect if one node is pointing to the other in spritekit


I have created two nodes one circle with 4 segments and one rotating bar. when the user touches the screen I want to detect if the bar is pointing to the right color or the wrong ones.

I tried checking the distance between the nodes but the bar's position is not changing and also the distance is always the same. I am not sure what I am missing.

enter image description here

I am creating the circle and bar like this

Bar
    colorBar = createImage()
    changeColor()
    colorBar!.colorBlendFactor = 1.0
    colorBar!.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 50, height: 300))
    colorBar!.physicsBody?.affectedByGravity = false
    colorBar!.position = CGPoint(x: self.frame.midX , y: self.frame.midY)
    colorBar!.anchorPoint = CGPoint(x: 0.5, y: 1.0)
    colorWheel!.addChild(colorBar!)

Circle

    for i in 0...3 {
        let section = SKShapeNode(path: path.cgPath)
        section.fillColor = colors![i]
        section.strokeColor = colors![i]
        section.name = colorNames[i]
        section.zRotation = rotationFactor * CGFloat(i);
        container.addChild(section)
    }

Solution

  • Instead of using one image for your circle, divide your image into 4 nodes and assign a physicsbody to all of them separately. This way you can detect whether your colorbar node is touching the specific colour node .