迅速移动背景无尽的4 [英] move a background endless in swift 4
问题描述
我正试图无休止地移动背景,但是当我模拟代码时,我遇到了一个小问题.
Hi i'm trying to move a background endlessly, but i have a little problem when i simulate the 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)
}
}
和
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
}
}
}
我得到了这个模拟结果: https://gyazo.com/63ce327ce9bc0a14199f411ac187de25
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 .
推荐答案
这是我用来无限滚动背景的代码(假设您正在使用SpriteKit?),您需要两个背景来赋予它外观和感觉.无限滚动的背景.我将其更新为可在3种背景下使用,以提供无限滚动.
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.
didMove(toView:)
方法之外
var bg = SKSpriteNode()
var bg2 = SKSpriteNode()
var bg3 = SKSpriteNode()
var parallax = SKAction()
在didMove(toView:)
方法中设置背景
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)
最后,在每个帧之前更新的update(currentTime:)
方法中
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.
这篇关于迅速移动背景无尽的4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!