将背景移动到无限滚动帮助Swift Sprite Kit

时间:2015-07-23 17:25:02

标签: swift background sprite-kit

我无法理解我在这里做错了什么。它应该无限向下滚动,我无法解决它。救命啊!

 func moveBackground() {

    let moveBackground = SKAction.moveByX(0, y: -self.size.height, duration: 1.0)
    let repeatBackground = SKAction.repeatActionForever(moveBackground)
    let removeBackground = SKAction.removeFromParent()
    let sequenceThis = SKAction.sequence([moveBackground, repeatBackground, removeBackground])
}
func repeatBackground() {

    let generateBackground = SKAction.sequence([
        SKAction.runBlock(self.moveBackground),
        SKAction.waitForDuration(2.7)])
    let endlessAction = SKAction.repeatActionForever(generateBackground)
    runAction(endlessAction)
}



override func didMoveToView(view: SKView) {
    scene?.scaleMode = .AspectFill


    var backgroundTexture = SKTexture(imageNamed: "Background.png")
    background.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2)
    background.texture = backgroundTexture
    background.size.width = self.frame.size.width
    background.size.height = self.frame.size.height
    self.addChild(background)

}

每当我运行应用程序时,背景只会保持在其位置并且不会移动。

2 个答案:

答案 0 :(得分:0)

首先moveBackground()除了创建动作之外什么也不做,它不会运行它们。您可能忘记最后运行runAction(sequenceThis)

还有很多问题,例如,你是因为某种原因删除精灵,但是让动作永远重复,它会再次移除精灵,但没有任何东西将其添加回来。

repeatBackground()永远运行moveBackground(),但它不会创建新的背景,只会永远移动一个。

最后didMoveToView不会拨打repeatBackground()moveBackground()

答案 1 :(得分:0)

moveBackground()中你没有“正在”行动。结果可能是它什么也没做。

您需要告诉后台运行此操作。

backgroundView.runAction(sequenceThis)