如何检测我是否点击了SKShapeNode

时间:2017-06-09 21:58:52

标签: ios swift xcode sprite-kit

覆盖func didMove(查看:SKView){

    view.scene?.anchorPoint = CGPoint(x: 0,y : 0)

    castle = SKShapeNode(circleOfRadius: ballRadius1)
    castle.physicsBody = SKPhysicsBody(circleOfRadius:ballRadius1)
    castle.fillColor = .white
    castle.name = "castle"
    castle.position = CGPoint(x: 0, y: 0)
    castle.physicsBody?.isDynamic = false
    castle.physicsBody?.affectedByGravity = false;
    castle.isUserInteractionEnabled = true
    self.addChild(castle)

我在屏幕中间有一个圆圈,我想点击它时它会消失。你能帮我么?它是一个SKShapeNode,并且具有半径相同的PhysicsBody

1 个答案:

答案 0 :(得分:1)

您可以使用touchesBegan并使其看起来像这样

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {


    for touch: AnyObject in touches{

        let pointOfTouch = touch.location(in: self)
        let nodeITapped = atPoint(pointOfTouch)
        let nameOfTappedNode = nodeITapped.name

        if nameOfTappedNode == "castle"{
            //make it do whatever you want
            castle.removeFromParent()

        }

    }


}

如果您希望用户在发布触摸后立即消失,您也可以在touchesEnded中实现它。