如何让相机跟随Sprite Kit中的SKNode?

Dvo*_*ole 12 ios sprite-kit

我希望背景固定不同的物体(精灵和其他物体).

玩家将由将在世界各地移动的中心节点代表.我想让我的相机固定在中央节点,例如世界应该移动,相机应保持静止.

我如何实现这一目标?

GOR*_*GOR 16

新的SKCameraNode似乎是一种非常简单的方法.

https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKCameraNode/

只是

1)创建一个SKCameraNode

// properties of scene class
let cam = SKCameraNode()
let player = SKSpriteNode()
Run Code Online (Sandbox Code Playgroud)

2)将其添加到场景摄像机属性中

// In your scene, for instance didMoveToView
self.camera = cam
Run Code Online (Sandbox Code Playgroud)

3)让相机跟随你的播放器位置

override func update(currentTime: CFTimeInterval)
{
    /* Called before each frame is rendered */
    cam.position = player.position
}
Run Code Online (Sandbox Code Playgroud)


Gre*_*sak 13

我意识到这与Stuart Welsh的回答相似; 但是,我想让他的答案更加全面.

基本上,Apple建议您创建一个"世界"节点来包含所有其他节点,世界是实际场景的子节点; 此外,您创建一个"相机"节点作为世界节点的子节点.

然后,从您的场景中,您可以执行以下操作:

[self centerOnCameraNamed:myCameraName];
Run Code Online (Sandbox Code Playgroud)

哪个调用方法(也在你的场景中):

- (void)centerOnCameraNamed:(NSString*)cameraName
{
  SKNode* world = [self childNodeWithName:worldName];
  SKNode* camera = [world childNodeWithName:cameraName];
  CGPoint cameraPositionInScene = [camera.scene convertPoint:camera.position fromNode:world];
  world.position = CGPointMake(world.position.x - cameraPositionInScene.x, world.position.y - cameraPositionInScene.y);
}
Run Code Online (Sandbox Code Playgroud)


vau*_*all 6

Swift 5.1中使用SKCameraNodewithSKConstraint

let camera = SKCameraNode()
camera.constraints = [.distance(.init(upperLimit: 10), to: node)]
scene.addChild(camera)
scene.camera = camera
Run Code Online (Sandbox Code Playgroud)


小智 5

这样的事情应该会有所帮助.我从didSimulatePhysics中调用以下内容.

-(void)centerOnNode:(SKNode*)node {
    CGPoint cameraPositionInScene = [node.scene convertPoint:node.position fromNode:node.parent];
    cameraPositionInScene.x = 0;
    node.parent.position = CGPointMake(node.parent.position.x - cameraPositionInScene.x, node.parent.position.y - cameraPositionInScene.y);
}
Run Code Online (Sandbox Code Playgroud)