Reputation: 17
I am trying to develop a simple game, with that game i need a infinite scrolling background. It is working for 2 times, and after the image has scrolled for 2 times it is adding the image to late and not with the right position.
Here is my code right now.
Move Background function
func moveBackground() {
let backgroundVelocity : CGFloat = 10.0
self.enumerateChildNodesWithName("background", usingBlock: { (node, stop) -> Void in
if let bg = node as? SKSpriteNode {
bg.position = CGPoint(x: bg.position.x, y: bg.position.y - backgroundVelocity)
// Checks if bg node is completely scrolled off the screen, if yes, then puts it at the end of the other node.
if bg.position.y <= -bg.size.height {
bg.position = CGPointMake(bg.position.x , bg.position.y + bg.size.width * 2)
}
}
})
}
Initialize Scrolling Background function
func initializingScrollingBackground() {
self.addChild(background)
for var index = 0; index < 2; index++ {
let bg = SKSpriteNode(imageNamed: "bg")
bg.position = CGPoint(x: 0 , y: index * Int(bg.size.height))
bg.anchorPoint = CGPointZero
bg.name = "background"
bg.zPosition = 10
self.addChild(bg)
}
}
Hope you guys can help me out with this.
Upvotes: 1
Views: 993
Reputation: 72760
In this line:
bg.position = CGPointMake(bg.position.x , bg.position.y + bg.size.width * 2)
^^^^^^^^^^^^^
shouldn't you use height
and not width
?
Upvotes: 2