Swift 3 - 后台循环无法正常工作

Swift 3 - Background loop not working properly

我正在尝试制作一款类似 Flappy Bird 的游戏,并且必须让我的背景无限循环。我在网上找到了一些教程,但它对我不起作用,我也不知道为什么。

到目前为止,这是我的代码

var Background = SKSpriteNode()
var txtBG = SKTexture(imageNamed: "Background")
var txtLand = SKTexture(imageNamed: "Ground")
var Ground = SKSpriteNode()
var moving:SKNode!


// Cena

override func didMove(to view: SKView) {

    scene?.anchorPoint = CGPoint(x: 0, y: 0)

    moving = SKNode()
    self.addChild(moving)


    //Ground

    Ground = SKSpriteNode(texture: txtLand)
    Ground.position = CGPoint(x:0, y: txtLand.size().height * 1.3)
    Ground.setScale(2.5)
    self.addChild(Ground)




    //Background

    Background = SKSpriteNode(texture: txtBG)
    Background.setScale(2.0)
    Background.position = CGPoint(x: self.size.width, y: txtLand.size().height * 5.2 )

    let BGmove = SKAction.moveBy(x: -txtBG.size().width * 2.0, y: txtLand.size().height * 5.2 , duration: TimeInterval(15))
    let BGRepo = SKAction.moveBy(x: txtBG.size().width * 2.0, y: txtLand.size().height * 5.2, duration: 0.0)
    let BGLoop = SKAction.repeatForever(SKAction.sequence([BGmove,BGRepo]))

    for i in 0 ..< 2 + Int(self.frame.size.width / ( txtBG.size().width * 2 )) {
        let i = CGFloat(i)
        Background = SKSpriteNode(texture: txtBG)
        Background.setScale(2.0)
        Background.position = CGPoint(x:txtBG.size().width/2 + txtBG.size().width * i * 2.0, y: txtLand.size().height * 5.2)
        Background.run(BGLoop)
        self.addChild(Background)
    }

发生的事情是背景开始水平移动但也向上移动,我不明白为什么。

任何帮助将不胜感激!

您的背景移动操作使背景移动 x 和 y。如果你想使用 SKAction.moveBy(x: CGFloat, y: CGFloat, duration: TimeInterval) 然后将 y 设置为 0 或者你可以使用 SKAction.moveTo(x: CGFloat, duration: TimeInterval) 只移动 x 值。