【发布时间】:2018-03-07 22:45:41
【问题描述】:
您好,我正在尝试无休止地移动背景,但是在模拟代码时遇到了一点问题。
这是我的代码
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
我的问题是黑色背景,我不知道如何替换它以使背景无限移动。
【问题讨论】:
-
这非常简单。我将从我的游戏中发布我的代码,向您展示我是如何做到的。
标签: swift background sprite-kit