I am making a simple game in SpriteKit and I have a scrollable background. What just happens is that when the game scene loads, several background images are placed next to each other, and then the image moves horizontally when it scrolls from the screen. Here is the code for this, from my game scene didMoveToView.
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)
}
Now I want to increase the speed of background scrolling at certain points in the game. You can see that the background horizontal scroll duration is set to (20 / self.gameSpeed). Obviously, this does not work, because this code is run only once, and therefore the movement speed is never updated to take into account the new value of the variable self.gameSpeed.
, : ( ) self.gameSpeed?
!