How to make camera follow SKNode in Sprite Kit?

后端 未结 6 2088
庸人自扰
庸人自扰 2020-12-08 23:21

I want to have background stationary with different objects (sprites and other objects).

Player will be represented by center node that will move around the world. I

6条回答
  •  情深已故
    2020-12-08 23:53

    I realize this is similar to Stuart Welsh's answer; however, I'd like to make his good answer a bit more comprehensive.

    Basically, Apple recommends that you create a "world" node to contain all of your other nodes, with the world being a child of the actual scene; and further that you create a "camera" node as a child of the world node.

    Then, from your scene, you can do something like:

    [self centerOnCameraNamed:myCameraName];
    

    Which calls the method (also in your scene):

    - (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);
    }
    

提交回复
热议问题