ios – Sprite Kit相机在节点上

前端之家收集整理的这篇文章主要介绍了ios – Sprite Kit相机在节点上前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我做了一个平台游戏,并将相机指向主节点播放器.

我已经通过了可以在这里找到的苹果文档:Apple

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

我得到了一个Player节点,每当屏幕被触摸时,它都会得到一个冲动(玩家跳起来).整个游戏只能在y轴上移动.

我创建的摄像机专注于播放器,每当播放器跳转平台向下移动并且播放器向上移动时.但是,这只是视野,而不是现实世界的立场.这意味着,SpawnPlatforms中给定的位置总是相同的,因为它们不移动,只有坚持使用Player的View上下移动.

我想让我的播放器节点上的相机,它在y轴上移动,而在它进一步向上,平台也应该向下移动,只有视图改变,我不能使用更新:重建平台的方法.我该怎么改呢?

编辑:我的代码使用更新:方法中的更新,但这仍然不能回答我的初始问题,如何移动整个世界与相机,不仅相机.

  1. override func didMoveToView(view: SKView) {
  2. /* Setup your scene here */
  3.  
  4. self.physicsWorld.contactDelegate = self
  5. self.anchorPoint = CGPoint(x: 0,y: 0.30)
  6.  
  7. backgroundColor = SKColor(red: 81.0/255.0,green: 192.0/255.0,blue: 201.0/255.0,alpha: 1.0)
  8.  
  9. self.World = SKNode()
  10. self.World.name = "World"
  11. addChild(World)
  12.  
  13. self.Camera = SKNode()
  14. self.Camera.name = "Camera"
  15. self.World.addChild(Camera)
  16.  
  17. SpawnPlatforms()
  18. SpawnPlayer()
  19. }
  20.  
  21. func SpawnPlatforms() {
  22.  
  23. Platform0 = SKSpriteNode (color: SKColor.greenColor(),size: CGSize(width: self.frame.size.width,height: 25))
  24. Platform0.position = CGPoint(x: self.frame.size.width / 2,y: -36)
  25. Platform0.zPosition = 1
  26.  
  27. Platform0.physicsBody = SKPhysicsBody(rectangleOfSize:Platform0.size)
  28. Platform0.physicsBody?.dynamic = false
  29. Platform0.physicsBody?.allowsRotation = false
  30. Platform0.physicsBody?.restitution = 0
  31. Platform0.physicsBody?.usesPreciseCollisionDetection = true
  32.  
  33. Platform0.physicsBody?.categoryBitMask = Platform0Category
  34. Platform0.physicsBody?.collisionBitMask = PlayerCategory
  35. Platform0.physicsBody?.contactTestBitMask = PlayerCategory
  36.  
  37. World.addChild(Platform0)
  38.  
  39. ....//Same code for the other Platforms1-5
  40. }
  41.  
  42. func SpawnPlayer(){
  43.  
  44. Player = SKSpriteNode (imageNamed: "Image.png")
  45. Player.size = CGSize(width: 64,height: 64)
  46. Player.position = CGPoint(x: self.frame.size.width / 2,y: 0)
  47. Player.zPosition = 2
  48.  
  49. Player.physicsBody = SKPhysicsBody(rectangleOfSize:CGSize(width: 35,height: 50))
  50. Player.physicsBody?.dynamic = true
  51. Player.physicsBody?.allowsRotation = false
  52. Player.physicsBody?.restitution = 0
  53. Player.physicsBody?.usesPreciseCollisionDetection = true
  54.  
  55. Player.physicsBody?.categoryBitMask = PlayerCategory
  56. Player.physicsBody?.collisionBitMask = Platform0Category
  57. Player.physicsBody?.contactTestBitMask = Platform0Category | Platform1Category | Platform2Category | Platform3Category | Platform4Category | Platform5Category
  58.  
  59. World.addChild(Player)
  60.  
  61. }
  62.  
  63. override func didSimulatePhysics() {
  64.  
  65. Camera.position = CGPoint(x: Player.position.x,y: Player.position.y)
  66.  
  67. self.centerOnNode(Camera!)
  68.  
  69. }
  70.  
  71. func centerOnNode(node: SKNode) {
  72.  
  73. let cameraPositionInScene: CGPoint = Camera.scene!.convertPoint(Camera.position,fromNode: World)
  74.  
  75. World.runAction(SKAction.moveTo(CGPoint(x:World.position.x,y:World.position.y - cameraPositionInScene.y),duration: 1.0))
  76.  
  77. }
  78.  
  79. override func update(currentTime: CFTimeInterval) {
  80. /* Called before each frame is rendered */
  81.  
  82. if(Player.position.y > (Platform3.position.y + 30) && Player.position.y < Platform4.position.y){
  83. Platform1.position = CGPointMake(self.frame.size.width / 2,Platform6.position.y + self.frame.size.height / 4.5)
  84. Platform1.color = SKColor.redColor()
  85. }
  86.  
  87. ...//Same code for the other Platforms1-5
  88. }

解决方法

创建一个SKNode,让我们称之为map.添加地图作为这个场景的孩子.您添加每个其他游戏节点作为此地图的一个孩子(平台,播放器等).

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

此外,您可以添加游戏菜单到场景,他们不会移动,但地图将.

例如,下一个代码将以线性速度移动update:method中的map:

kScrollScenarioFactor可能就像200.f

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

或者如果要根据播放器在update:方法中移动地图,可以查看此方法

  1. -(void)update:(CFTimeInterval)currentTime {
  2. //Calculating playerPosition difference
  3. float diffX = self.lastPlayerPosition.x - self.player.position.x;
  4. float diffY = self.lastPlayerPosition.y - self.player.position.y;
  5. self.lastPlayerPosition = self.player.position;
  6.  
  7. //Updating map position
  8. self.map.position = CGPointMake(map.position.x + diffX,map.position.y + diffY);
  9. }

猜你在找的iOS相关文章