I have an SCNScene for a 3D game I am building. I have positioned a HUD over this scene using SCNScene.overlaySKScene.
我正在制作一个3D游戏的SCNScene。我用SCNScene.overlaySKScene在这个场景上设置了一个HUD。
In order to receive taps on my 3D scene, I use a gesture recogniser and hitTest and this works fine.
为了在3D场景中接收轻拍,我使用了手势识别器和hitTest,效果很好。
I am trying to determine the SKSpriteNodes that I have added to the HUD SKScene on taps now. This does not work with the gesture recogniser.
我正在试着确定我在水龙头上添加的SKSpriteNodes。这对手势识别器不起作用。
Having looked at the WWDC 2014 Banana sample, I don't think I am doing anything all that different in my use of the alternative touchesBegan method (below).
在查看了WWDC 2014香蕉样本后,我认为我在使用touchesBegan方法时没有做任何不同的事情(如下)。
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
println("touchesBegan")
let scnView = self.view as SCNView
let skScene = scnView.overlaySKScene
let touch: AnyObject? = touches.anyObject()
let p = touch?.locationInNode(skScene)
let touchedNode = skScene.nodeAtPoint(p!)
println("touchedNode \(touchedNode.name)")
super.touchesBegan(touches, withEvent: event)
}
The 2nd println is finding a nil result.
第二个println正在寻找一个nil结果。
I create by HUD:
我创建了住房和城市发展部:
scnView.overlaySKScene = SKScene(fileNamed: "HUDScene.sks")
scnView.overlaySKScene.name = "HUD"
scnView.overlaySKScene.delegate = self
I then add a root SKNode to this scene
然后在这个场景中添加一个根SKNode
var hudRoot = SKNode()
And add SKSpriteNodes with userInteractionEnabled=true to this hudRoot.
并添加使用userInteractionEnabled=true的SKSpriteNodes =对这个hudRoot。
touchesBegan is being called, but nil is returned when touching the SKSpriteNodes. Interestingly, if I touch outside of the SKSpriteNodes and NOT on any 3D nodes, then it returns the hudRoot node even though that is defaulted to userInteractionEnabled=false.
touchesBegan被调用,但是在触摸SKSpriteNodes时返回nil。有趣的是,如果我在SKSpriteNodes之外操作,而不是在任何3D节点上操作,那么它将返回hudRoot节点,即使它默认为userInteractionEnabled=false。
Now that I have checked my implementation with Apple's own Bananas example, I am without any remaining ideas.
现在我已经用苹果自己的香蕉例子检查了我的实现,我没有任何其他的想法。
1 个解决方案
#1
1
Try changing:
试着改变:
let p = touch?.locationInNode(skScene)
let touchedNode = skScene.nodeAtPoint(p!)
To:
:
let p = touch?.locationInNode(hudRoot)
let touchedNode = hudRoot.nodeAtPoint(p!)
The reason I suggest this is because the nodes you're trying to interact with are children of hudRoot and therefore their positions are in hudRoots coordinate system. The touch location you're currently getting is in the skScene's coordinate system.
我建议这样做的原因是,因为您试图与之交互的节点是hudRoot的子节点,因此它们的位置在hudRoots坐标系中。当前获得的触摸位置位于skScene的坐标系统中。
If the above doesn't solve your problem try logging the touch location to the console and comparing what you're getting with what you're expecting.
如果上面的方法不能解决您的问题,请尝试将触摸位置记录到控制台,并将您所得到的与期望的进行比较。
Hopefully this helps or at least gives you a hint on what your problem may be.
希望这对你有所帮助,或者至少给你一个提示,告诉你你的问题是什么。
#1
1
Try changing:
试着改变:
let p = touch?.locationInNode(skScene)
let touchedNode = skScene.nodeAtPoint(p!)
To:
:
let p = touch?.locationInNode(hudRoot)
let touchedNode = hudRoot.nodeAtPoint(p!)
The reason I suggest this is because the nodes you're trying to interact with are children of hudRoot and therefore their positions are in hudRoots coordinate system. The touch location you're currently getting is in the skScene's coordinate system.
我建议这样做的原因是,因为您试图与之交互的节点是hudRoot的子节点,因此它们的位置在hudRoots坐标系中。当前获得的触摸位置位于skScene的坐标系统中。
If the above doesn't solve your problem try logging the touch location to the console and comparing what you're getting with what you're expecting.
如果上面的方法不能解决您的问题,请尝试将触摸位置记录到控制台,并将您所得到的与期望的进行比较。
Hopefully this helps or at least gives you a hint on what your problem may be.
希望这对你有所帮助,或者至少给你一个提示,告诉你你的问题是什么。