覆盖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

最佳答案

您可以使用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中实现它。

关于ios - 如何检测我是否轻按了SKShapeNode,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44467247/

10-08 23:09