Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在swift中使用SKConstraint_Ios_Swift_Sprite Kit - Fatal编程技术网

Ios 如何在swift中使用SKConstraint

Ios 如何在swift中使用SKConstraint,ios,swift,sprite-kit,Ios,Swift,Sprite Kit,我的场景中有一个移动的摄像机,它总是跟着我的播放器。但是我也有一些其他的内容(屏幕上的控件),我想把它们放在屏幕上的一个地方,但是当相机移动时,这些控件就会移动到另一个位置。我该怎么做呢。我搜索了很多,找到了SKConstraint,但是我找不到任何在swift 3中使用它的教程 我应该使用SKConstraint?如果是,我如何使用它;如果不是,我如何始终将控件保持在屏幕上的某个位置 我也知道我可以在update方法中更改控件的位置,但我不想这样做,因为屏幕上有很多控件,我尽量避免在updat

我的场景中有一个移动的摄像机,它总是跟着我的播放器。但是我也有一些其他的内容(屏幕上的控件),我想把它们放在屏幕上的一个地方,但是当相机移动时,这些控件就会移动到另一个位置。我该怎么做呢。我搜索了很多,找到了
SKConstraint
,但是我找不到任何在swift 3中使用它的教程

我应该使用
SKConstraint
?如果是,我如何使用它;如果不是,我如何始终将控件保持在屏幕上的某个位置

我也知道我可以在update方法中更改控件的位置,但我不想这样做,因为屏幕上有很多控件,我尽量避免在update方法中编写代码


任何帮助都将不胜感激,谢谢

可以使用
SKConstraint
使摄影机跟随角色

首先,创建摄影机节点和英雄,并将它们添加到场景中

let cameraNode = SKCameraNode()
let hero = SKSpriteNode(imageNamed: "Spaceship")

addChild(hero)
camera = cameraNode
addChild(cameraNode)
接下来,创建约束并将其指定给摄影机节点的
约束
属性

let range = SKRange(constantValue:0)
let constraint = SKConstraint.distance(range, to: hero)
cameraNode.constraints = [constraint]
最后,如果控件或标签需要位于相对于摄影机的固定位置,则可以将它们添加到摄影机节点

let label = SKLabelNode(text: "Score: 123")

// Position the label relative to the camera node
label.position = CGPoint(x: 100, y: 100)
cameraNode.addChild(label)

可以使用
SKConstraint
使摄影机跟随角色

首先,创建摄影机节点和英雄,并将它们添加到场景中

let cameraNode = SKCameraNode()
let hero = SKSpriteNode(imageNamed: "Spaceship")

addChild(hero)
camera = cameraNode
addChild(cameraNode)
接下来,创建约束并将其指定给摄影机节点的
约束
属性

let range = SKRange(constantValue:0)
let constraint = SKConstraint.distance(range, to: hero)
cameraNode.constraints = [constraint]
最后,如果控件或标签需要位于相对于摄影机的固定位置,则可以将它们添加到摄影机节点

let label = SKLabelNode(text: "Score: 123")

// Position the label relative to the camera node
label.position = CGPoint(x: 100, y: 100)
cameraNode.addChild(label)

非常感谢您的帮助,所以诀窍是将节点添加到SKCameraNode。非常感谢您的帮助,所以诀窍是将节点添加到SKCameraNode。