ios - 节点上的 Sprite Kit 相机

标签 ios swift camera sprite-kit

我做了一个平台游戏,把摄像头对准了主节点Player。

我已经阅读了可以在这里找到的 Apple 文档:Apple

大约有 6 个平台,它们之间有 self.frame.size.height/4.5 空间,它们应该在每次低于 dy = -15 时重生再次在屏幕顶部,使其无限循环。

我有一个 Player 节点,每次触摸屏幕时它都会产生一个脉冲(Player 跳上跳下)。整个游戏只在 y 轴 上移动。

我创建的相机聚焦在玩家身上,每次玩家跳跃时平台都会向下移动,而玩家会向上移动。然而,这只是 View ,而不是真实世界的位置。这意味着,SpawnPlatforms 中的给定位置始终相同,因为它们不会移动,只有粘附在 Player 上的 View 会上下移动。

我想在我的播放器节点上安装摄像头,它在 y 轴上移动,当它进一步向上移动时,平台也应该向下移动,目前只有 View 发生变化,我无法使用更新:重生平台的方法。我该如何改变它?

编辑:我的代码与 Update:Method 中的更新一起工作,但这仍然没有回答我最初的问题,如何移动整个世界与相机,而不仅仅是相机。

override func didMoveToView(view: SKView) {
    /* Setup your scene here */

    self.physicsWorld.contactDelegate = self
    self.anchorPoint = CGPoint(x: 0, y: 0.30)

    backgroundColor = SKColor(red: 81.0/255.0, green: 192.0/255.0, blue: 201.0/255.0, alpha: 1.0)

    self.World = SKNode()
    self.World.name = "World"
    addChild(World)

    self.Camera = SKNode()
    self.Camera.name = "Camera"
    self.World.addChild(Camera)

    SpawnPlatforms()
    SpawnPlayer()
}

func SpawnPlatforms() {

    Platform0 = SKSpriteNode (color: SKColor.greenColor(), size: CGSize(width: self.frame.size.width , height: 25))
    Platform0.position = CGPoint(x: self.frame.size.width / 2, y: -36)
    Platform0.zPosition = 1

    Platform0.physicsBody = SKPhysicsBody(rectangleOfSize:Platform0.size)
    Platform0.physicsBody?.dynamic = false
    Platform0.physicsBody?.allowsRotation = false
    Platform0.physicsBody?.restitution = 0
    Platform0.physicsBody?.usesPreciseCollisionDetection = true

    Platform0.physicsBody?.categoryBitMask = Platform0Category
    Platform0.physicsBody?.collisionBitMask = PlayerCategory
    Platform0.physicsBody?.contactTestBitMask = PlayerCategory

    World.addChild(Platform0)

    ....//Same code for the other Platforms1-5
    }

    func SpawnPlayer(){

    Player = SKSpriteNode (imageNamed: "Image.png")
    Player.size = CGSize(width: 64, height: 64)
    Player.position = CGPoint(x: self.frame.size.width / 2, y: 0)
    Player.zPosition = 2

    Player.physicsBody = SKPhysicsBody(rectangleOfSize:CGSize(width: 35, height: 50))
    Player.physicsBody?.dynamic = true
    Player.physicsBody?.allowsRotation = false
    Player.physicsBody?.restitution = 0
    Player.physicsBody?.usesPreciseCollisionDetection = true

    Player.physicsBody?.categoryBitMask = PlayerCategory
    Player.physicsBody?.collisionBitMask = Platform0Category
    Player.physicsBody?.contactTestBitMask = Platform0Category | Platform1Category | Platform2Category | Platform3Category | Platform4Category | Platform5Category

    World.addChild(Player)

}

override func didSimulatePhysics() {

    Camera.position = CGPoint(x: Player.position.x, y: Player.position.y)

    self.centerOnNode(Camera!)

}

func centerOnNode(node: SKNode) {

    let cameraPositionInScene: CGPoint = Camera.scene!.convertPoint(Camera.position, fromNode: World)

    World.runAction(SKAction.moveTo(CGPoint(x:World.position.x , y:World.position.y - cameraPositionInScene.y), duration: 1.0))

}

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */

    if(Player.position.y > (Platform3.position.y + 30) && Player.position.y < Platform4.position.y){
        Platform1.position = CGPointMake(self.frame.size.width / 2, Platform6.position.y + self.frame.size.height / 4.5)
        Platform1.color = SKColor.redColor()
    }

    ...//Same code for the other Platforms1-5
 }

最佳答案

创建一个SKNode,我们称它为 map 。添加 map 作为该场景的子项。您将所有其他游戏节点添加为该 map 的子节点(平台、玩家等)。

然后,不是移动 node.parent,而是移动 map 。这将导致即使您移动相机,平台也始终处于相同的位置。

此外,您可以将游戏菜单添加到场景中,它们不会移动,但 map 会移动。

例如,下一个代码将在 update: 方法中以线速度移动 map :

kScrollScenarioFactor 可能类似于 200.f

-(void)update:(CFTimeInterval)currentTime {
  //Calculating time for realistic scroll speed
  float scrollTime = currentTime - self.lastTime;
  self.lastTime = currentTime;
  self.map.position = CGPointMake(map.position.x - (scrollTime * self.kScrollScenarioFactor), map.position.y);
}

或者如果你想在 update: 方法中根据玩家移动 map ,你可以看看这个方法:

-(void)update:(CFTimeInterval)currentTime {
  //Calculating playerPosition difference
  float diffX = self.lastPlayerPosition.x - self.player.position.x;
  float diffY = self.lastPlayerPosition.y - self.player.position.y;
  self.lastPlayerPosition = self.player.position;

  //Updating map position
  self.map.position = CGPointMake(map.position.x + diffX, map.position.y + diffY);
}

关于ios - 节点上的 Sprite Kit 相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31159041/

相关文章:

java - 安卓相机,焦点 slider

ios - 自动布局:UIImageView 不随设备方向旋转

swift - 继承 SLKTextViewController

iphone - 有没有办法找到 UICollectionView 部分的索引?

camera - 移动相机关闭后,Three.js buffergeometry 消失

Android - 拍摄的照片与预览不同

iOS/tvOS 根据操作系统版本使用不同的应用程序扩展

ios - iOS 8 嵌入式框架中的头文件

ios - 使用 iOS Game Center Challenge 发送对象?

ios - 折线图无法正确显示