检测SpriteKit中对象子节点的触摸

时间:2021-09-14 10:40:52

I have a custom class that is an SKNode, which in turn has several SKSpriteNodes in it. Is there a way I can detect touches on these child SKSpriteNodes from my game scene?

我有一个自定义类,它是一个SKNode,它反过来有几个SKSpriteNodes。有没有一种方法可以让我从我的游戏场景中检测到这些儿童SKSpriteNodes ?

I'm working in swift

我工作在斯威夫特

3 个解决方案

#1


9  

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {

   let touch = touches.anyObject() as UITouch

   let touchLocation = touch.locationInNode(self)

    if([yourSprite containsPoint: touchLocation])
    {
         //sprite contains touch
    }
}

Source: http://www.raywenderlich.com/84434/sprite-kit-swift-tutorial-beginners

来源:http://www.raywenderlich.com/84434/sprite-kit-swift-tutorial-beginners

#2


1  

Examine Apple's SceneKitVehicle demo. Someone kindly ported it to Swift.

检查苹果SceneKitVehicle演示。有人好心地把它移植到Swift。

The code you want is in the GameView.swift file. In the GameView you'll see the touchesBegan override. Here's my version of it for Swift 2.1:

你想要的代码在GameView中。快速文件。在GameView中,你会看到touchesBegan覆盖。以下是我对Swift 2.1的版本:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    guard let scene = self.overlaySKScene else {
        return
    }

    let touch = touches.first!
    let viewTouchLocation = touch.locationInView(self)
    let sceneTouchPoint = scene .convertPointFromView(viewTouchLocation)
    let touchedNode = scene.nodeAtPoint(sceneTouchPoint)

    if (touchedNode.name == "Play") {
        print("play")
    }
}

If it's not clear; the GameView is set as the app's view class by way of the Storyboard.

如果不清楚;GameView被设置为app的视图类通过故事板。

#3


0  

You'll need to compare the location of your Touch to the location of your SKNode

您需要将您的触摸位置与SKNode的位置进行比较

You can get the location of your touch at one of the following methods, using locationInNode():

使用locationInNode(),您可以在以下方法中获得触摸的位置:

  • touchesBegan()
  • touchesBegan()
  • touchesMoved()
  • 出来()
  • touchesEnded()
  • touchesEnded()

#1


9  

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {

   let touch = touches.anyObject() as UITouch

   let touchLocation = touch.locationInNode(self)

    if([yourSprite containsPoint: touchLocation])
    {
         //sprite contains touch
    }
}

Source: http://www.raywenderlich.com/84434/sprite-kit-swift-tutorial-beginners

来源:http://www.raywenderlich.com/84434/sprite-kit-swift-tutorial-beginners

#2


1  

Examine Apple's SceneKitVehicle demo. Someone kindly ported it to Swift.

检查苹果SceneKitVehicle演示。有人好心地把它移植到Swift。

The code you want is in the GameView.swift file. In the GameView you'll see the touchesBegan override. Here's my version of it for Swift 2.1:

你想要的代码在GameView中。快速文件。在GameView中,你会看到touchesBegan覆盖。以下是我对Swift 2.1的版本:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    guard let scene = self.overlaySKScene else {
        return
    }

    let touch = touches.first!
    let viewTouchLocation = touch.locationInView(self)
    let sceneTouchPoint = scene .convertPointFromView(viewTouchLocation)
    let touchedNode = scene.nodeAtPoint(sceneTouchPoint)

    if (touchedNode.name == "Play") {
        print("play")
    }
}

If it's not clear; the GameView is set as the app's view class by way of the Storyboard.

如果不清楚;GameView被设置为app的视图类通过故事板。

#3


0  

You'll need to compare the location of your Touch to the location of your SKNode

您需要将您的触摸位置与SKNode的位置进行比较

You can get the location of your touch at one of the following methods, using locationInNode():

使用locationInNode(),您可以在以下方法中获得触摸的位置:

  • touchesBegan()
  • touchesBegan()
  • touchesMoved()
  • 出来()
  • touchesEnded()
  • touchesEnded()