逐渐提高 SpriteKit 中滚动背景的速度

Gradually increasing speed of scrolling background in SpriteKit

我正在用 SpriteKit 制作一个简单的游戏,我有一个滚动背景。简单的情况是,当游戏场景加载时,几张背景图像彼此相邻放置,然后当图像滚出屏幕时,图像水平移动。这是来自我的游戏场景的 didMoveToView 方法的代码。

// self.gameSpeed is 1.0 and gradually increases during the game

let backgroundTexture = SKTexture(imageNamed: "Background")
var moveBackground = SKAction.moveByX(-self.frame.size.width, y: 0, duration: (20 / self.gameSpeed))
var replaceBackground = SKAction.moveByX(self.frame.size.width, y: 0, duration: 0)
var moveBackgroundForever = SKAction.repeatActionForever(SKAction.sequence([moveBackground, replaceBackground]))

for var i:CGFloat = 0; i < 2; i++ {
    var background = SKSpriteNode(texture: backgroundTexture)
    background.position = CGPoint(x: self.frame.size.width / 2 + self.frame.size.width * i, y: CGRectGetMidY(self.frame))
    background.size = self.frame.size
    background.zPosition = -100
    background.runAction(moveBackgroundForever)

    self.addChild(background)
}

现在我想在游戏的某些点提高滚动背景的速度。可以看到背景水平滚动的时长设置为(20 / self.gameSpeed)。显然这是行不通的,因为这段代码只有 运行 一次,因此移动速度永远不会更新以说明 self.gameSpeed 变量的新值。

所以,我的问题很简单:如何根据 self.gameSpeed 变量提高背景图像移动的速度(减少持续时间)?

谢谢!

你可以利用这样的东西

SKAction.waitforDuration(a certain amount of period to check for the updated values) 

SKAction.repeatActionForever(the action above)
runAction(your action)
{ // this is the completion block, do whatever you want here, check the values and adjust them accordly
}

您可以使用gameSpeed变量来设置背景的速度。为此,首先,您需要参考您的两个背景片段(如果需要,可以参考更多):

class GameScene: SKScene {
    lazy var backgroundPieces: [SKSpriteNode] = [SKSpriteNode(imageNamed: "Background"), 
                                                 SKSpriteNode(imageNamed: "Background")]

    // ... 
}

现在您需要 gameSpeed 变量:

var gameSpeed: CGFloat = 0.0 {
    // Using a property observer means you can easily update the speed of the 
    // background just by setting gameSpeed.
    didSet {
        for background in backgroundPieces {
            // Minus, because the background is moving from left to right.
            background.physicsBody!.velocity.dx = -gameSpeed
        }
    }
}

然后将每个棋子正确放置在didMoveToView中。此外,要使此方法起作用,每个背景片段都需要一个物理体,以便您可以轻松更改其速度。

override func didMoveToView(view: SKView) {
    for (index, background) in enumerate(backgroundPieces) {
        // Setup the position, zPosition, size, etc...

        background.physicsBody = SKPhysicsBody(rectangleOfSize: background.size)
        background.physicsBody!.affectedByGravity = false
        background.physicsBody!.linearDamping = 0
        background.physicsBody!.friction = 0

        self.addChild(background)
    }

    // If you wanted to give the background and initial speed,
    // here's the place to do it.
    gameSpeed = 1.0
}

您可以在 update 中更新 gameSpeed,例如 gameSpeed += 0.5

最后,在 update 中,您需要检查背景片段是否离开屏幕(向左)。如果有,则需要将其移至背景片段链的末尾:

override func update(currentTime: CFTimeInterval) {
    for background in backgroundPieces {
        if background.frame.maxX <= 0 {
            let maxX = maxElement(backgroundPieces.map { [=13=].frame.maxX })

            // I'm assuming the anchor of the background is (0.5, 0.5)
            background.position.x = maxX + background.size.width / 2
        }
    }
}