Hi i'm trying to move a background endlessly, but i have a little problem when i simulate the code.
This is my code
didMove(to view: SKView) {
for i in 0...1 {
let backgroundNEW = SKSpriteNode(imageNamed: "New_Background")
backgroundNEW.size = self.size
backgroundNEW.anchorPoint = CGPoint(x: 0.5 , y: 0)
backgroundNEW.position = CGPoint(x: self.size.width , y: self.size.height * CGFloat(i))
backgroundNEW.zPosition = -1
backgroundNEW.name = "test"
addChild(backgroundNEW)
}
}
and
var lastUpdateTime : TimeInterval = 0
var deltaFrameTime : TimeInterval = 0
var amountToMovePerSecond : CGFloat = 200.0
override func update(_ currentTime: TimeInterval){
if lastUpdateTime == 0{
lastUpdateTime = currentTime
}
else {
deltaFrameTime = currentTime - lastUpdateTime
lastUpdateTime = currentTime
}
let amountToMoveBackground = amountToMovePerSecond * CGFloat(deltaFrameTime)
self.enumerateChildNodes(withName: "test") {
backgroundNEW, stop in
backgroundNEW.position.x -= amountToMoveBackground
if backgroundNEW.position.x < -self.size.height{
backgroundNEW.position.x += self.size.height*2
}
}
}
i got this simulation : https://gyazo.com/63ce327ce9bc0a14199f411ac187de25
my problem is the black background i dont know how i can replace it to do the background moving endless .
Here is my code I am using to scroll a background infinitely (assuming you are using SpriteKit?) You need two backgrounds to give it the look and feel of an infinitely scrolling background. I updated it to work for 3 backgrounds to provide an infinite scroll.
outside your
didMove(toView:)
methodsetting up your backgrounds inside your
didMove(toView:)
methodNow to handle the scrolling of the backgrounds
Finally, in the
update(currentTime:)
method that updates before each frameIf you have any questions, ask them so I can help you out.