问题
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 .
回答1:
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:)
method
var bg = SKSpriteNode()
var bg2 = SKSpriteNode()
var bg3 = SKSpriteNode()
var parallax = SKAction()
setting up your backgrounds inside your didMove(toView:)
method
bg = SKSpriteNode(imageNamed: "texture")
bg.position = CGPoint(x: 0, y:0)
bg.zPosition = 3
bg.size = CGSize(width:Int, height:Int) //make sure to set the width to self.frame.size.width, the height can be anything
bg2 = SKSpriteNode(imageNamed: "texture")
bg2.position = CGPoint(x: self.frame.size.width, y:0)
bg2.zPosition = 3
bg2.size = CGSize(width:Int, height:Int) //make sure to set the width to self.frame.size.width, the height can be anything
bg3 = SKSpriteNode(imageNamed: "texture")
bg3.position = CGPoint(x: self.frame.size.width + bg2.position.x, y:0)
bg3.zPosition = 3
bg3.size = CGSize(width:Int, height:Int) //make sure to set the width to self.frame.size.width, the height can be anything
self.addChild(bg)
self.addChild(bg2)
self.addChild(bg3)
Now to handle the scrolling of the backgrounds
parallax = SKAction.repeatForever(SKAction.move(by: CGVector(dx: -self.frame.size.width, dy: 0), duration: 20))
//higher duration moves it slower, lower duration moves it faster
bg.run(parallax)
bg2.run(parallax)
bg3.run(parallax)
Finally, in the update(currentTime:)
method that updates before each frame
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
if bg.position.x <= -self.frame.size.width {
bg.position.x = self.frame.size.width * 2
//this ensures that your backgrounds line up perfectly
}
if bg2.position.x <= -self.frame.size.width {
bg2.position.x = self.frame.size.width * 2
//this ensures that your backgrounds line up perfectly
}
if bg3.position.x <= -self.frame.size.width {
bg3.position.x = self.frame.size.width * 2
//this ensures that your backgrounds line up perfectly
}
}
If you have any questions, ask them so I can help you out.
来源:https://stackoverflow.com/questions/49162476/move-a-background-endless-in-swift-4