SKShapeNode似乎缺少触动

时间:2018-06-14 15:13:23

标签: ios skshapenode

我想知道某位专家是否可以帮我解决初学者的问题。我想使用SKShapeNodes来表示非矩形形状,但需要检测它们上的触摸。

我下面的代码似乎不起作用。我错过了一些微不足道的事情吗?

import SpriteKit

class GameScene: SKScene , SKPhysicsContactDelegate {

    var ball: SKShapeNode!

    override func didMove(to view: SKView) {
        backgroundColor = .white
        ball = createBall()
        ball.position = CGPoint.zero
        addChild(ball)
    }

    func createBall() -> SKShapeNode {

        let path = CGMutablePath()
        path.addArc(center: CGPoint.zero,
                    radius: 35,
                    startAngle: 0,
                    endAngle: CGFloat.pi * 2,
                    clockwise: true)
        let ball = SKShapeNode(path: path,  centered: true)
        ball.lineWidth = 1
        ball.fillColor = .lightGray
        ball.strokeColor = .black
        ball.isUserInteractionEnabled = true
        return ball
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches {
            let location = touch.location(in: self)
            print("\(location)")   // diagnostic - produces output whenever click is NOT within SKShapeNode

            if ball.contains(location) {
                print("\(location)") // diagnostic - never produces output!!
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

删除以下代码行:-

ball.isUserInteractionEnabled = true 

对我来说很好。