如何将SpriteKit Sprite节点的可点击区域从矩形更改为圆形?

时间:2020-11-11 20:25:26

标签: ios swift sprite-kit geometry skspritenode

我的菜单屏幕上有一个播放按钮,但是它可以检测到整个矩形区域中的触摸,而不仅仅是圆形。是否可以在xcode中将可点击范围更改为圆形?

或者我将不得不在初始化或函数内以编程方式进行操作吗?

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

PlayButton

1 个答案:

答案 0 :(得分:0)

只需以编程方式将其添加到GameScene。

   var playButton: SKSpriteNode!



   override func didMove(to view: SKView) {
        
         playButton = SKSpriteNode(imageNamed: "playButton")
         playButton.name = "playButton"
         playButton.position = CGPoint(x: self.frame.midX , y: self.frame.midY)
         playButton.zPosition = 0
         playButton.alpha = 1

         addChild(playButton)
   }



  override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let touch = touches.first else {
        return
    }
    
    let pos = touch.location(in: self)
    let node = self.atPoint(pos)

    if node.name == "playButton"{

       //Do Something
    }
     

  }