Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在swift 4中移动背景_Swift_Background_Sprite Kit - Fatal编程技术网

在swift 4中移动背景

在swift 4中移动背景,swift,background,sprite-kit,Swift,Background,Sprite Kit,嗨,我正在尝试无休止地移动背景,但我在模拟代码时遇到了一个小问题 这是我的密码 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

嗨,我正在尝试无休止地移动背景,但我在模拟代码时遇到了一个小问题

这是我的密码

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
变量deltaFrameTime:TimeInterval=0
var amountToMovePerSecond:CGFloat=200.0
覆盖函数更新(uCurrentTime:TimeInterval){
如果lastUpdateTime==0{
lastUpdateTime=currentTime
}
否则{
deltaFrameTime=currentTime-lastUpdateTime
lastUpdateTime=currentTime
}
设amountToMoveBackground=amountToMovePerSecond*CGFloat(deltaFrameTime)
self.enumerateChildNodes(名称为“test”){
背景新的,请稍候
backgroundNEW.position.x-=移动背景的数量
如果背景为New.position.x<-self.size.height{
backgroundNEW.position.x+=自身尺寸高度*2
}
}
}
我得到了这个模拟:


我的问题是黑色背景,我不知道如何替换它以使背景不断移动。

这是我用来无限滚动背景的代码(假设您使用的是SpriteKit?),您需要两个背景,使其具有无限滚动背景的外观和感觉。我将其更新为3个背景,以提供一个无限卷轴

在您的
didMove(toView:)
方法之外

var bg = SKSpriteNode()
var bg2 = SKSpriteNode()
var bg3 = SKSpriteNode()

var parallax = SKAction()
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)
didMove(toView:)
方法中设置背景

var bg = SKSpriteNode()
var bg2 = SKSpriteNode()
var bg3 = SKSpriteNode()

var parallax = SKAction()
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)
现在来处理背景的滚动

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:)
方法中

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
    }
}
覆盖函数更新(\uCurrentTime:TimeInterval){
//在渲染每个帧之前调用

如果bg.position.x,这非常简单。我将发布我游戏中的代码,向您展示我是如何做到的。您好。谢谢您的回复我尝试了您的代码,但他对我来说效果不好,我得到了这个,我将bg=SKSpriteNode(纹理:“纹理”)替换为let background=SKSpriteNode(ImageName:“background”)和let background2=SKSpriteNode(ImageName:“background2”)对于我得到的这个background2.size CGSize(宽度:self.frame.size.width,高度:self.frame.size.height)你知道我如何修复它吗?我使用Xcode 9和Sprite套件。如果它正常工作,你只需要添加第三个背景。我将编辑上面的答案,向你展示如何添加第三个背景。我编辑的答案已经过测试,效果非常好。