【问题标题】:SceneKit basicsSceneKit 基础知识
【发布时间】:2014-11-28 02:08:21
【问题描述】:

我刚刚开始尝试使用 SceneKit。尝试一个非常简单的代码来创建一个对象,给它分配物理特性,然后点击它给它一个速度。到目前为止,我能够创建它,应用物理。我能够给它速度,但不能通过点击来实现。我究竟做错了什么?我使用的是在 SpriteKit 中使用的相同技术,但它在 SceneKit 中不起作用。请帮忙。

class GameViewController: UIViewController, SCNSceneRendererDelegate, SCNPhysicsContactDelegate {
let scene = SCNScene()

let shipNode = SCNNode()

let sceneView = SCNScene()

var cameraNode = SCNNode()
var floor = SCNFloor()
var floornod = SCNNode()

override func viewDidLoad() {
    super.viewDidLoad()

    let sceneView = self.view as SCNView
    sceneView.scene = scene
    sceneView.backgroundColor = SKColor.blueColor()
    sceneView.showsStatistics = true


    sceneView.scene?.physicsWorld.gravity = SCNVector3Make(0, -70, 0)
    sceneView.scene?.physicsWorld.speed = 1.0
    sceneView.delegate = self
    sceneView.jitteringEnabled = true
    sceneSetup()
    sceneView.pointOfView = cameraNode


    let ship = SCNScene(named: "art.scnassets/sphere.dae")!

    let shipNode = ship.rootNode.childNodeWithName("Sphere", recursively: true)!

    shipNode.position = SCNVector3Make(0, 50, 300)
    shipNode.scale = SCNVector3Make(10, 10, 10)
    shipNode.geometry?.firstMaterial?.diffuse.contents = UIImage(named: "ball.jpg")
    shipNode.physicsBody = SCNPhysicsBody(type: SCNPhysicsBodyType.Dynamic, shape: nil)
    shipNode.physicsBody?.restitution = 0.9
    shipNode.physicsBody?.velocity = SCNVector3Make(0, 100, 10)
    scene.rootNode.addChildNode(shipNode)
    println(shipNode.physicsBody?.velocity.y)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    shipNode.physicsBody?.velocity = SCNVector3Make(0, 200, 20) // code not working
    println(shipNode.physicsBody?.velocity.y) // prints nil instead of 200.00
}

    func sceneSetup() {

    cameraNode.camera = SCNCamera()
    cameraNode.camera?.zNear = 0.01
    cameraNode.camera?.zFar = 800
    cameraNode.camera?.xFov = 45
    cameraNode.camera?.yFov = 75


    cameraNode.rotation = SCNVector4Make(1, 0, 0, -25 * 0.017453293)
    cameraNode.position = SCNVector3Make(0, 150, 500)
    scene.rootNode.addChildNode(cameraNode)

    floornod.geometry = floor
    floornod.geometry?.firstMaterial?.diffuse.contents = "wood1.png"
    floornod.geometry?.firstMaterial?.locksAmbientWithDiffuse = true
    floornod.geometry?.firstMaterial?.diffuse.wrapS = SCNWrapMode.Repeat
    floornod.geometry?.firstMaterial?.diffuse.wrapT = SCNWrapMode.Repeat
    floornod.geometry?.firstMaterial?.diffuse.mipFilter = SCNFilterMode.Linear
    floornod.physicsBody = SCNPhysicsBody(type: SCNPhysicsBodyType.Static, shape: nil)
    floornod.position = SCNVector3Make(0, 0, 0)

    scene.rootNode.addChildNode(floornod)

}

}

第一个 println 给出的结果是 100.00。 touchesBegan 中的 println 每次点击时都会给出 nil 的结果。

非常感谢任何帮助。谢谢。

【问题讨论】:

    标签: swift scenekit


    【解决方案1】:

    您在 viewDidLoad 方法中使用 let shipNode = ... 定义了一个新的局部变量。这个得到一个物理体并被放入场景中。

    这与您定义为类的一部分的实例变量shipNode 是分开的。那个永远不会得到任何进一步的配置,所以当你在touchesBegan 中访问它时,它没有物理体,并且可选链接导致查询其速度的整个表达式返回 nil。

    大概您想摆脱viewDidLoad 中的lets,以便配置实例变量而不是隐藏它们。

    【讨论】:

    • 哇。我不敢相信我错过了。谢谢。成功了!
    猜你喜欢
    • 1970-01-01
    • 2014-04-16
    • 2018-05-25
    • 2014-02-14
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多