快速移动背景4

问题描述 投票:-1回答:1

嗨我正在尝试无休止地移动背景,但是当我模拟代码时我有一点问题。

这是我的代码

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

我的问题是黑色的背景,我不知道我怎么能取代它来做背景移动无穷无尽。

swift background sprite-kit
1个回答
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
    }
}

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

© www.soinside.com 2019 - 2024. All rights reserved.