快速移动背景4

时间:2018-03-07 22:45:42

标签: swift background sprite-kit

您好我试图无休止地移动背景,但是当我模拟代码时我遇到了一些问题。

这是我的代码

didMove(to view: SKView) {

    for i  in 0...1 {
        let backgroundNEW = SKSpriteNode(imageNamed: "New_Background")
        backgroundNEW.size = self.size
        backgroundNEW.anchorPoint = CGPoint(x: 0.5 , y: 0)
        backgroundNEW.position = CGPoint(x: self.size.width  ,  y: self.size.height  * CGFloat(i))
        backgroundNEW.zPosition = -1
        backgroundNEW.name = "test"
        addChild(backgroundNEW)
    }
}

 var lastUpdateTime : TimeInterval = 0
 var deltaFrameTime : TimeInterval = 0
 var amountToMovePerSecond : CGFloat = 200.0

override func update(_ currentTime: TimeInterval){

    if lastUpdateTime == 0{
        lastUpdateTime = currentTime
    }
    else {
        deltaFrameTime = currentTime - lastUpdateTime
        lastUpdateTime = currentTime
    }

    let amountToMoveBackground =  amountToMovePerSecond * CGFloat(deltaFrameTime)

    self.enumerateChildNodes(withName: "test") {
        backgroundNEW, stop in

        backgroundNEW.position.x -= amountToMoveBackground

        if backgroundNEW.position.x < -self.size.height{
            backgroundNEW.position.x += self.size.height*2
        }
    }
}

我得到了这个模拟:https://gyazo.com/63ce327ce9bc0a14199f411ac187de25

我的问题是黑色背景我不知道如何替换它来使背景移动无穷无尽。

1 个答案:

答案 0 :(得分:0)

这是我用来无限滚动背景的代码(假设你使用SpriteKit?)你需要两个背景来赋予它无限滚动背景的外观和感觉。我更新它以适用于3个背景以提供无限滚动。

超出didMove(toView:)方法

var bg = SKSpriteNode()
var bg2 = SKSpriteNode()
var bg3 = SKSpriteNode()

var parallax = SKAction()

didMove(toView:)方法

中设置背景
bg = SKSpriteNode(imageNamed: "texture")
bg.position = CGPoint(x: 0, y:0)
bg.zPosition = 3
bg.size = CGSize(width:Int, height:Int) //make sure to set the width to self.frame.size.width, the height can be anything

bg2 = SKSpriteNode(imageNamed: "texture")
bg2.position = CGPoint(x: self.frame.size.width, y:0)
bg2.zPosition = 3
bg2.size = CGSize(width:Int, height:Int) //make sure to set the width to self.frame.size.width, the height can be anything

bg3 = SKSpriteNode(imageNamed: "texture")
bg3.position = CGPoint(x: self.frame.size.width + bg2.position.x, y:0)
bg3.zPosition = 3
bg3.size = CGSize(width:Int, height:Int) //make sure to set the width to self.frame.size.width, the height can be anything

self.addChild(bg)
self.addChild(bg2)
self.addChild(bg3)

现在处理背景的滚动

parallax = SKAction.repeatForever(SKAction.move(by: CGVector(dx: -self.frame.size.width, dy: 0), duration: 20)) 
//higher duration moves it slower, lower duration moves it faster

bg.run(parallax)
bg2.run(parallax)
bg3.run(parallax)

最后,在每个帧之前更新的update(currentTime:)方法

override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered
    if bg.position.x <= -self.frame.size.width {
        bg.position.x = self.frame.size.width * 2
        //this ensures that your backgrounds line up perfectly 
    }
    if bg2.position.x <= -self.frame.size.width {
        bg2.position.x = self.frame.size.width * 2
        //this ensures that your backgrounds line up perfectly
    }
    if bg3.position.x <= -self.frame.size.width {
        bg3.position.x = self.frame.size.width * 2
        //this ensures that your backgrounds line up perfectly
    }
}

如果您有任何疑问,请询问他们,以便我帮助您。