如何在spriteKit swift 4中创建无限背景

时间:2018-03-26 20:50:44

标签: swift3 sprite-kit skspritenode

我尝试通过节点创建无尽的背景,但背景没有变得无限并且被打断,第三个背景还没有显示出来。在第一个节目之后,场景中的节点数量增加了,如何解决这个问题呢?

enter image description here

enter image description here

enter image description here

import SpriteKit
import GameplayKit

class GameScene: SKScene {


   var bgNode: SKNode!
   var overlay: SKNode!
   var overlayWidth: CGFloat!
   //var viewSize: CGSize!
   var levelPositionX: CGFloat = 0.0
   //var speed: CGFloat = 5.5


   override func didMove(to view: SKView) {
    setupNode()
    //viewSize = CGSize(width: frame.size.width, height: 

                                              frame.size.height )

  }

  func setupNode() {
    let worldNode = childNode(withName: "World")!
    bgNode = worldNode.childNode(withName: "Background")!
    overlay = bgNode.childNode(withName: "Overlay")!.copy() as! 
  SKNode
    overlayWidth = overlay.calculateAccumulatedFrame().width
  }

   func createBackgroundOverlay() {
    let backgroundOverlay = overlay.copy() as! SKNode
    backgroundOverlay.position = CGPoint(x: 0.0, y: 0.0)

    bgNode.addChild(backgroundOverlay)
    levelPositionX += overlayWidth
}

func update() {
    bgNode.position.x -= 5
    if bgNode.position.x <= -self.frame.size.width {
         bgNode.position.x = self.frame.size.width * 2
        createBackgroundOverlay()

      }
  }

  override func update(_ currentTime: TimeInterval) {
      update()
  }    

1 个答案:

答案 0 :(得分:0)

在我无尽的亚军游戏中,我实现了无穷无尽的背景和地面(或地板),与您的应用程序非常相似。下面我将讨论我在游戏中使用的步骤。

第1步:在GameScene.swift文件中添加这些变量。

var backgroundSpeed: CGFloat = 80.0 // speed may vary as you like
var deltaTime: TimeInterval = 0
var lastUpdateTimeInterval: TimeInterval = 0

第2步:在GameScene文件中,按如下方式制作 setUpBackgrouds 方法

func setUpBackgrounds() {
    //add background

    for i in 0..<3 {
        // add backgrounds, my images were namely, bg-0.png, bg-1.png, bg-2.png

        let background = SKSpriteNode(imageNamed: "bg-\(i).png")
        background.anchorPoint = CGPoint.zero
        background.position = CGPoint(x: CGFloat(i) * size.width, y: 0.0)
        background.size = self.size
        background.zPosition = -5
        background.name = "Background"
        self.addChild(background)

    }

    for i in 0..<3 {
        // I have used one ground image, you can use 3
        let ground = SKSpriteNode(imageNamed: "Screen.png")
        ground.anchorPoint = CGPoint(x: 0, y: 0)
        ground.size = CGSize(width: self.size.width, height: ground.size.height)
        ground.position = CGPoint(x: CGFloat(i) * size.width, y: 0)
        ground.zPosition = 1
        ground.name = "ground"
        self.addChild(ground)

    }
}

第3步:现在我们必须从更新方法

中捕获timeIntervals
override func update(_ currentTime: TimeInterval) {
    if lastUpdateTimeInterval == 0 {
        lastUpdateTimeInterval = currentTime
    }

    deltaTime = currentTime - lastUpdateTimeInterval
    lastUpdateTimeInterval = currentTime
}

第4步:这是最重要的部分,通过枚举子节点来移动我们的背景和groungFloor。在GameScene.swift文件中添加这两个方法。

func updateBackground() {
    self.enumerateChildNodes(withName: "Background") { (node, stop) in

        if let back = node as? SKSpriteNode {
            let move = CGPoint(x: -self.backgroundSpeed * CGFloat(self.deltaTime), y: 0)
            back.position += move

            if back.position.x < -back.size.width {
                back.position += CGPoint(x: back.size.width * CGFloat(3), y: 0)
            }
        }

    }
}

func updateGroundMovement() {
    self.enumerateChildNodes(withName: "ground") { (node, stop) in

        if let back = node as? SKSpriteNode {
            let move = CGPoint(x: -self.backgroundSpeed * CGFloat(self.deltaTime), y: 0)
            back.position += move

            if back.position.x < -back.size.width {
                back.position += CGPoint(x: back.size.width * CGFloat(3), y: 0)
            }
        }

    }
}

第5步:此时您应该收到此错误:&#34;二元运算符&#39; + =&#39;不适用于两个CGPoint&#39;操作数&#34;在updateBackground和updateGroundMovement方法中。 Error Messages

现在我们需要实现运算符重载来解决这个问题。创建一个新的Swift文件并将其命名为Extensions.swift,然后按如下方式实现:

//  Extensions.swift

import CoreGraphics
import SpriteKit

public func + (left: CGPoint, right: CGPoint) -> CGPoint {
   return CGPoint(x: left.x + right.x, y: left.y + right.y)
}

public func += (left: inout CGPoint, right: CGPoint) {
   left = left + right
}

步骤6:在didMove中调用 setUpBackgrounds 方法(toView :)

override func didMove(to view: SKView) {

    setUpBackgrounds()

}

第7步:最后在update(_ currentTime)方法中调用 updateBackground updateGroundMovement 方法。更新的代码如下:

override func update(_ currentTime: TimeInterval) {
    if lastUpdateTimeInterval == 0 {
        lastUpdateTimeInterval = currentTime
    }

    deltaTime = currentTime - lastUpdateTimeInterval
    lastUpdateTimeInterval = currentTime

    //MARK:- Last step:- add these methods here
    updateBackground()
    updateGroundMovement()
}