【发布时间】:2017-05-01 05:43:22
【问题描述】:
我希望 bobNode 查看用户在屏幕上点击的位置。我正在努力旋转 bobNode。我正在记录点击位置,当我点击时它可以工作。我从来没有收到print("Angle: " + String(describing: angle))。唯一一次print("Testing Renderer function") 是应用程序加载时,它打印了 11 次,之后不再打印。我想我对旋转 bobNode 的概念感到困惑。如果我做错了,有人可以向我解释如何去做。
代码:
var sceneView: SCNView! // for now
var bobNode: SCNNode!
var lastTouch: CGPoint? = nil
let bobScene = SCNScene(named: "art.scnassets/man.scn")!
func start(){
let scene = SCNScene()
sceneView.delegate = self
bobNode = bobScene.rootNode.childNode(withName: "Player", recursively: true)!
bobNode.scale = SCNVector3(x: 0.15, y: 0.15, z: 0.15)
bobNode.eulerAngles = SCNVector3(x: GLKMathDegreesToRadians(75), y: 0.0, z: GLKMathDegreesToRadians(180))//Don't use rotaion THAT shit is retarded
scene.rootNode.addChildNode(bobNode)
sceneView.scene = scene
let camera = SCNCamera()
let cameraNode = SCNNode()
cameraNode.camera = camera
cameraNode.position = SCNVector3(x: bobNode.position.x, y: bobNode.position.y, z: 10.0)
scene.rootNode.addChildNode(cameraNode)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
handleTouches(touches: touches)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
handleTouches(touches: touches)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
handleTouches(touches: touches)
}
private func handleTouches(touches: Set<UITouch>) {
for touch in touches {
let touchLocation = touch.location(in: sceneView)
lastTouch = touchLocation
print("Touch: " + String(describing: lastTouch))
}
}
func renderer(_ renderer: SCNSceneRenderer, didSimulatePhysicsAtTime time: TimeInterval) {
if let _ = bobNode {
updatePlayer()
}
}
func updatePlayer() {
print("Testing Renderer function")
if let touch = lastTouch {
let currentPosition = bobNode.position
let angle = atan2(CGFloat(currentPosition.y) - CGFloat(touch.y), CGFloat(currentPosition.x) - CGFloat(touch.x)) + CGFloat(Double.pi)
print("Angle: " + String(describing: angle))
let rotateAction = SCNAction.rotateBy(x: 0.0, y: angle, z: 0.0, duration: 0)
bobNode.runAction(rotateAction)
}
}
【问题讨论】: