import UIKitimport SceneKitclass GameVIEwController: UIVIEwController {var scnVIEw:SCNVIEw!var scnScene:SCNScene!var sprite: spritekitHUD!var cameraNode: SCNNode!var shape: SCNNode!overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() setupScene() }func setupScene() { scnVIEw = self.vIEw as! SCNVIEw scnVIEw.delegate = self scnVIEw.allowsCameraControl = true scnScene = SCNScene(named: "art.scnassets/scene.scn") scnVIEw.scene = scnScene sprite=spritekitHUD(size: self.vIEw.bounds.size,game: self) scnVIEw.overlaySKScene=sprite cameraNode = scnScene.rootNode.childNode(withname: "camera",recursively: true)! shape=scnScene.rootNode.childNode(withname: "shape",recursively: true) shape.name="ThreeDShape" } overrIDe func touchesBegan(_ touches: Set<UItouch>,with event: UIEvent?){ let touch = touches.first! let location = touch.location(in: scnVIEw) let hitResults = scnVIEw.hitTest(location,options: nil) if let result = hitResults.first { handletouchFor(node: result.node) } }func handletouchFor(node: SCNNode) { if node.name == "ThreeDShape" { print("SCNNode touched") } }}
这是我的Spritekit叠加场景
import Foundationimport SpriteKitclass spritekitHUD: SKScene{var game:GameVIEwController!var shapeNode: SKSpriteNode!init(size: CGSize,game: GameVIEwController){ super.init(size: size) self.backgroundcolor = UIcolor.white let Spritesize = size.wIDth/12 self.shapeNode= SKSpriteNode(imagenamed: "shapeNode") self.shapeNode.size = CGSize(wIDth: Spritesize,height: Spritesize) self.shapeNode.position = CGPoint(x: Spritesize + 8,y: Spritesize + 8) self.shapeNode.name="test" self.game=game self.addChild(self.pauseNode)}required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder)}overrIDe func touchesEnded(_ touches: Set<UItouch>,with event: UIEvent?) { guard let touch=touches.first else{ return } let location=touch.location(in: self) if self.atPoint(location).name=="test" { print("Spritekit node pressed") } }}
所以有了这个,我可以成功获得通知,我的spritenode已经触及我的overlaySKScene,但我无法弄清楚如何获得我的SCNode被触摸的通知.如果你不能有2个touchesbegan函数,有没有人有任何想法如何我同时处理2d事件的3D事件?
谢谢你的帮助!!
这是从Xcode的游戏模板中直接“解除”……在vIEwDIDLoad中添加手势识别器:
// add a tap gesture recognizer let tapGesture = UITapGestureRecognizer(target: self,action: #selector(handleTap(_:))) scnVIEw.addGestureRecognizer(tapGesture) func handleTap(_ gestureRecognize: UIGestureRecognizer) { // retrIEve the SCNVIEw let scnVIEw = self.vIEw as! SCNVIEw // check what nodes are tapped let p = gestureRecognize.location(in: scnVIEw) let hitResults = scnVIEw.hitTest(p,options: [:]) // check that we clicked on at least one object if hitResults.count > 0 { // retrIEved the first clicked object let result: AnyObject = hitResults[0] // result.node is the node that the user tapped on // perform any actions you want on it }}总结
以上是内存溢出为你收集整理的从SCNScene和overlaySKScene获取触摸事件的swift / scenekit问题全部内容,希望文章能够帮你解决从SCNScene和overlaySKScene获取触摸事件的swift / scenekit问题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)