Swift 3 - 背景循环不能正常工作

时间:2017-03-24 15:41:49

标签: swift sprite-kit skaction

我正在努力制作像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)
    }

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

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

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