SceneKit项目用户交互



我一直在SceneKit中开发一款游戏,该游戏涉及一个用户可以使用箭头键控制的平面。飞机可以飞行,但箭头键对飞机没有影响。当我运行一个测试来查看箭头键功能是否工作时,我可以看到箭头键有效地改变了飞机的速度,但飞机没有增加或降低速度。这是代码:

import SceneKit
import QuartzCore
class GameViewController: NSViewController {
var height = 0
var speed = 0 //I should be able to change this value by pressing the up arrow key during the simulation
override func keyDown(with theEvent: NSEvent) {
if(theEvent.keyCode == 123){//left
}
if(theEvent.keyCode == 124){//right
}
if(theEvent.keyCode == 125){//down
}
if(theEvent.keyCode == 126){//up
speed+=1
print(speed)
}
}
override func viewDidLoad(){
super.viewDidLoad()
let scene = SCNScene(named: "art.scnassets/Plane.scn")!
let cameraNode = scene.rootNode.childNode(withName: "camera", recursively: true)!
let plane = scene.rootNode.childNode(withName: "plane", recursively: true)!
plane.runAction(SCNAction.repeatForever(SCNAction.moveBy(x: 0, y: CGFloat(height), z: CGFloat(speed), duration: 1)))
cameraNode.runAction(SCNAction.repeatForever(SCNAction.moveBy(x:0, y:CGFloat(height), z: CGFloat(speed), duration:1)))
let scnView = self.view as! SCNView
}
}

一旦创建了一个具有依赖于speed变量的参数的SCNAction对象,对speed的任何更改都不会更新操作。

这是因为speed是值类型(Int(,并且SCNAction实例和视图控制器的speed实例变量之间没有连接。这不是SceneKit特有的问题,任何以Int为参数的API都会以相同的方式运行。

最新更新