使用鼠标输入在场景周围正确移动SpriteKit节点

时间:2018-02-26 16:02:46

标签: swift macos sprite-kit scenekit sknode

我有一个SKScene的实例,我在我的macOS项目中使用SCNView作为叠加视图。我正在努力尝试在Sprite-Kit场景周围移动SKNode。我设置了一个手势识别器,用于检测是否发生了一次命中测试,以便SCNodes围绕适用于这些节点的SCNView移动:

//Handle press to select
@objc func handleTap(gestureRecognize:NSGestureRecognizer){
    let point = gestureRecognize.location(in: self.scnView)
    let hits = self.scnView.hitTest(point, options: nil)

    deselectObject() //empty tap

    for hit in hits{
        if hit.node != grid_node{
            selectObject(tappedNode: hit.node)
        }
    }
}

但是,当我选择SKNode时,命中测试结果会返回0,因为它只跟踪SKscene中的内容,而不是覆盖场景...所以,我尝试修改命中测试代码像这样:

    let hits = self.scnView.overlaySKScene?.view?.hitTest(point)

但是,然后出现以下错误:

    for hit in hits{
        if hit.node != grid_node{
            selectObject(tappedNode: hit.node)
        }
    }

所以,我的下一个想法是使用鼠标事件来跟踪所选的SKNode是什么,并将其拖动到叠加场景周围,如下所示:

override func mouseMoved(with event: NSEvent) {
    // Get mouse position in scene coordinates
    let location = event.location(in: self.overlayScene)
    selectedSKNode?.position = location
}

override func mouseDown(with event: NSEvent) {
    // Get mouse position in scene coordinates
    let location = event.location(in: self.overlayScene)
    // Get node at mouse position
    selectedSKNode? = self.overlayScene.atPoint(location)
    selectedSKNode?.position = location
}

override func mouseUp(with event: NSEvent) {
    // Get mouse position in scene coordinates
    let location = event.location(in: self.overlayScene)
    selectedSKNode?.position = location
}

然而,似乎没有调用mouseDownmouseUp方法 - 只有mouseMoved方法。

所以,我想我的问题在于几个不同的部分:

1)为什么不调用mouseDownmouseUp? 2)为什么修改我的hittest代码后会出现Type 'NSView?' does not conform to protocol 'Sequence'错误? 3)是否有某种方法来检测场景中检测到的节点,无论它是SCNode还是SKNode

0 个答案:

没有答案