如何让我的背景图像在Swift中永远重复?

时间:2015-01-24 18:01:46

标签: xcode swift

我有这两个图像是相同的图像,我希望它们重复,所以看起来背景正在移动。我有这个代码,但只有一个背景图像通过,然后它回到灰色屏幕。另一个背景图像不遵循第一个。我该如何解决这个问题?谢谢! (这是快速的xcode)

 func spawnBackground() {

    let cloud = SKSpriteNode(imageNamed: "CloudBg")
    let cloud2 = SKSpriteNode(imageNamed: "CloudBg2")


    cloud.position = CGPointMake(frame.size.width + cloud.frame.size.width / 2,cloud.frame.height / 2)
    cloud.setScale(1.35)
    cloud.zPosition = -15

    cloud2.position = CGPointMake(self.size.width/2, self.size.height/2)
    cloud2.setScale(1.35)
    cloud.zPosition = -15


    let moveTop = SKAction.moveByX(0, y: cloud.size.height, duration:   NSTimeInterval(CGFloat(gameSpeed) * cloud.size.height))
    cloud.runAction(SKAction.repeatActionForever(moveTop))
    cloud2.runAction(SKAction.repeatActionForever(moveTop))



    addChild(cloud)
    addChild(cloud2)


}

1 个答案:

答案 0 :(得分:1)

我认为您正在寻找这样的动作序列:

  let cloud = SKSpriteNode(imageNamed: "CloudBg")
        let cloud2 = SKSpriteNode(imageNamed: "CloudBg2")


        cloud.position = CGPointMake(frame.size.width + cloud.frame.size.width / 2,cloud.frame.height / 2)
        cloud.setScale(1.35)
        cloud.zPosition = -15

        cloud2.position = CGPointMake(self.size.width/2, self.size.height/2)
        cloud2.setScale(1.35)
        cloud.zPosition = -15

        var actionone = SKAction.moveToX(0, y: cloud.size.height, duration:   NSTimeInterval(CGFloat(gameSpeed) * cloud.size.height))

        var actiontwo = SKAction.runBlock({
            cloud.position = CGPointMake(frame.size.width + cloud.frame.size.width / 2,cloud.frame.height / 2)
        })

        cloud.runAction(SKAction.repeatForever(SKAction.sequence([actionone,actiontwo])))

当然,您必须编辑此代码才能在一个云到达某个点时开始工作,但这是一般的想法。