【问题标题】:Sprite Kit Camera on Node节点上的 Sprite Kit 相机
【发布时间】:2015-09-18 11:25:17
【问题描述】:

我做了一个平台游戏,把相机对准主节点Player。

我已经浏览了可以在此处找到的 Apple 文档:Apple

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

我有一个播放器节点,每次触摸屏幕时都会产生一个冲动(播放器上下跳跃)。整个游戏确实只在y-Axis上移动。

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

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

编辑:我的代码在 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
 }

【问题讨论】:

  • 你的问题有点不清楚。请详细说明。
  • 我编辑了我的原始帖子,希望现在更清楚。
  • 你试过阅读这篇文章吗? stackoverflow.com/questions/20828655/…
  • 您没有正确使用“相机”。 SO和Google上有很多很好的答案,展示了如何实现“相机”。 Xcode 7 实际上引入了一个相机节点类以使其更容易。
  • 我实际上是从 Apple Document 中得到这个“相机”的。你有新的 XCode 7 相机节点的链接吗,因为我找不到任何东西?

标签: ios swift camera sprite-kit


【解决方案1】:

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

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

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

例如,接下来的代码将在update:方法中以线速度移动地图:

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:方法中根据玩家移动地图,你可以看看这个方法:

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

【讨论】:

  • 所以,在我的代码中,我已经将所有内容添加到“世界”SKNode。如果我移动 World 而不是 node.parent!,我实际上没有发现任何差异。还是您的意思是我必须始终在相机下的更新方法中手动移动“世界”?
  • 忘记“相机”,直接将“世界”(或我的答案中的“地图”)向左移动。所以从技术上讲,视图将保持在相同的位置,但“世界”将向左移动(我假设玩家向右),使玩家留在屏幕内,其他一切都向左移动。
  • 我更新了“相机”,现在我移动世界而不是 node.parent!。我仍然无法使用更新:方法。还是我误会了你?
  • 更新答案以显示在 update: 方法中移动 map 的示例
  • 好的,现在我明白你的意思了,但我不想一直运动。相机应该聚焦在玩家身上,当相机向上移动时,平台应该以“真实”的方式向下移动,同样平滑,但只有在产生冲动之后。这就是为什么持续滚动对我来说不可用的原因。
【解决方案2】:

如果您有滚动背景,则在游戏中制作三层:

  • 前景层:包含玩家、平台和游戏中的任何其他节点。
  • 背景层:包含随着玩家移动而移动一定量的背景。
  • HUD:对于所有与得分相关的节点,无论背景和玩家位置如何,它们都将保持在其位置。

现在,根据我对您问题的理解,您所有的平台在场景中都有预定义的位置,这些位置不会改变。只有玩家节点的位置发生变化,因此您希望相机节点跟随玩家并随之移动世界。如果是这种情况,那么你可以试试这个:

SKNode 没有视觉内容,但在场景中有位置。这意味着您可以移动节点,其子节点将随之移动

最初添加以下类属性 -

var backgroundNode: SKNode!
var foregroundNode: SKNode!
var hudNode: SKNode!

var player: SKNode!
var platformType: PlatformType!

//添加背景节点

 func createBackgroundNode() -> SKNode {
    let backgroundNode = SKNode()
    let node = SKSpriteNode(imageNamed:"")

    node.position = CGPoint(x: xPosition , y: yPosition)
    backgroundNode.addChild(node)

    return backgroundNode
}

然后在游戏场景中调用这个

backgroundNode = createBackgroundNode()
addChild(backgroundNode)

现在添加播放器:

func createPlayer() -> SKNode {
  let playerNode = SKNode()
  playerNode.position = CGPoint(x: x.position, y: y.position)

  let sprite = SKSpriteNode(imageNamed: "Player")
  playerNode.addChild(sprite)

  return playerNode
}

为了添加播放器,我们将创建前景层,然后将其添加到其中。把它放在你添加 backgroundNode 的下面。

foregroundNode = SKNode()
addChild(foregroundNode) 

player = createPlayer()
foregroundNode.addChild(player)

最后,你要添加的平台,你可以添加为:

func createPlatformAtPosition(position: CGPoint) -> PlatformNode {

  let node = PlatformNode()
  let thePosition = CGPoint(x: position.x, y: position.y)
  node.position = thePosition
  node.name = "NODE_PLATFORM"

  var sprite: SKSpriteNode
  sprite = SKSpriteNode(imageNamed: "Platform")
  node.addChild(sprite)

  return node
} 

最后把这个添加到 Game Scene 下 foregroundNode.addChild(player)

  let platform = createPlatformAtPosition(CGPoint(x: 300, y: 270)
  foregroundNode.addChild(platform)

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多