本文介绍了Xcode 7 SceneKit 对象的位置属性在移动时不会更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在制作一个游戏,在上述游戏中,您将使用 DPad 在场景中移动您的球.我正在使用 Xcode 7 Beta 5,尽管在 beta 4 中也存在同样的问题.这是测试视图控制器的代码:

So I'm making a game and in said game, you will use a DPad to move your ball around the scene. I'm using Xcode 7 Beta 5 though the same problem existed in beta 4. Here is the code of a test view controller:

class TestController: UIViewController,DPadDelegate {
@IBOutlet var scnView:SCNView!
@IBOutlet var dpad:DPad!
var timer:NSTimer?
var update:NSTimer?
var colors = [UIColor.blueColor(),UIColor.redColor(),UIColor.whiteColor(),UIColor.yellowColor(),UIColor.cyanColor(),UIColor.orangeColor(),UIColor.magentaColor(),UIColor.purpleColor()]
var player:SCNNode!
var camera:SCNNode = SCNNode()
var i = 0
override func viewDidLoad() {
    super.viewDidLoad()
    NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: "ColorCubeUpdate", userInfo: nil, repeats: true)
    NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "updateCamera", userInfo: nil, repeats: true)
    dpad.delegate = self
    player = SCNNode(geometry: SCNSphere(radius: 0.25))
    player.geometry?.firstMaterial?.diffuse.contents = UIImage(named: "PlainEyes1")
    player.geometry?.firstMaterial?.multiply.contents = UIColor(red: 1.0, green: 0.5, blue: 0.5, alpha: 1.0)
    player.position = SCNVector3Make(0, 1.5, 0)
    player.physicsBody = SCNPhysicsBody(type: SCNPhysicsBodyType.Dynamic, shape: SCNPhysicsShape(geometry: player.geometry!, options: nil))
    scnView.scene?.rootNode.addChildNode(player)
    camera.camera = SCNCamera()
    let constraint = SCNLookAtConstraint(target: player)
    camera.constraints? = [constraint]
}

func updateCamera() {
    i += 1
    print(i)
    print("player's position (player.position) camera position (camera.position)")
    camera.position = player.position//SCNVector3Make(player.position.x+4, player.position.y+2, player.position.z)

}

func dPadDown() {
    player.physicsBody?.applyForce(SCNVector3(x: 1, y: 0, z: 0), atPosition: SCNVector3(x: 0, y: 0, z: 0), impulse: false)
}

func dPadUp() {
    player.physicsBody?.applyForce(SCNVector3(x: -1, y: 0, z: 0), atPosition: SCNVector3(x: 0, y: 0, z: 0), impulse: false)
}

func dPadLeft() {
    player.physicsBody?.applyForce(SCNVector3(x: 0, y: 0, z: 1), atPosition: SCNVector3(x: 0, y: 0, z: 0), impulse: false)
}

func dPadRight() {
    player.physicsBody?.applyForce(SCNVector3(x: 0, y: 0, z: -1), atPosition: SCNVector3(x: 0, y: 0, z: 0), impulse: false)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

}

colors 数组用于其他地方,而 i 变量仅用于调试,如您在 UpdateCamera 函数中所见.这一切看起来都很好,但这是我在 Xcode 中的输出,显示了我遇到的问题的根源:相机根本没有移动:

The colors array is used elsewhere an the i variable is just for debug as you can see in the UpdateCamera function. This all looks alright but here's my output in Xcode shows the source of a problem I'm having where the camera does not move at all:

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)50

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)50

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)51

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)51

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)52

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)52

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)53

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)53

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)54

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)54

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)55

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)55

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)56

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)56

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)57

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)57

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)58

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)58

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)59

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)59

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)60

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)60

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)61

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)61

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)62

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)62

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)63

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)63

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)64

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)64

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)65

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)65

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)66

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)66

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)67

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)67

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)68

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)68

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)69

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)69

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)70

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)70

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)71

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)71

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)72

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)72

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)73

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)73

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)74

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)74

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)75

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)75

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)76

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)76

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)77

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)77

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)78

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)78

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)79

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)79

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)80

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)80

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)81

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)81

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)82

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)82

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)83

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)83

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)84

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)84

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)85

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)85

玩家位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0) 摄像机位置 SCNVector3(x: 0.0, y: 1.5, z: 0.0)

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)

但是在我的设备上,我看到球落在地板上(在 scn 文件中制作)并按预期运行.唯一的问题是我无法访问玩家的实际位置,因此无法移动我的相机.

However on my device, I see the ball fall on a floor (made in a scn file) and act as expected. The only problem is that I don't have access to the actual position of the player and thus cannot move my camera.

我尝试过使用变换而不是位置,但也没有用.请帮忙!提前致谢!

I have tried using transform instead of position and that didn't work either. Please help!Thanks in advance!

推荐答案

你应该打印节点的presentationNodeposition.

这篇关于Xcode 7 SceneKit 对象的位置属性在移动时不会更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-14 23:51