除法 SpriteKit 的无限背景问题
Infinite Background problem with division SpriteKit
我一直在尝试实现无限背景动画,它应该在 4 个高度相同的图像之间切换,然后重复该序列。但是,它似乎无法正常工作。
备注anchorPoint = CGPoint(x: 0.5, y: 0)
func updateBackground(currentTime: TimeInterval){
var delta: CGFloat = 0.0
if lastUpdate != nil {
delta = CGFloat(currentTime - lastUpdate)
}
//First increment position
activeBackground1.position.y += delta*backgroundVelocity
activeBackground2.position.y += delta*backgroundVelocity
//Detect bounds surpass
if activeBackground1.position.y > activeBackground1.size.height + screen.height/2 {
lastSky = (lastSky + 1)%4
sky1 = SKTexture(imageNamed: "sky" + String(lastSky))
activeBackground1.texture = sky1
//Reposition: background1 new position is equal to minus the entire height of
//background2 + its y size.
activeBackground1.position.y = -abs(activeBackground2.size.height-activeBackground2.position.y)
}
if activeBackground2.position.y > activeBackground2.size.height + screen.height/2 {
lastSky = (lastSky + 1)%4
sky1 = SKTexture(imageNamed: "sky" + String(lastSky))
activeBackground2.texture = sky1
activeBackground2.position.y = -abs(activeBackground1.size.height-activeBackground1.position.y)
}
}
更新算法工作正常,但是当需要重新定位两个背景之一时,似乎从一个背景到另一个背景有大约 10.0 CGFloat
的偏移量。我做错了什么?
编辑:事实证明,错误位于我的图像中,它显示了一些空白行,因此产生了可视化故障。所以我的代码可以完美运行。
我做了测试,你很可能应该使用类似的东西:
activeBackground2.position.y = activeBackground1.size.height + activeBackground1.position.y
而不是
activeBackground2.position.y = -abs(activeBackground1.size.height-activeBackground1.position.y)
我做了这个例子,它工作正常:https://github.com/Maetschl/SpriteKitExamples/tree/master/InfiniteBackground/InfiniteBackground
随意看用。
您的问题是导致舍入错误的浮点数学运算。我现在在phone所以我不能写代码,但你想要做的是让 1 个父 SKNode 来处理你的整个背景。
将您的背景条添加到父节点。
然后,您仅将移动操作放在父项上。
当每条条子离开筛网时,您拿起条子,将其移动到其他条子的末端。
这种跳跃应该始终用整数数学来完成,不留空洞。
基本上:
浮点移动数学在父节点上完成。
基于整数的瓦片跳跃在每个条子上完成。
我一直在尝试实现无限背景动画,它应该在 4 个高度相同的图像之间切换,然后重复该序列。但是,它似乎无法正常工作。
备注anchorPoint = CGPoint(x: 0.5, y: 0)
func updateBackground(currentTime: TimeInterval){
var delta: CGFloat = 0.0
if lastUpdate != nil {
delta = CGFloat(currentTime - lastUpdate)
}
//First increment position
activeBackground1.position.y += delta*backgroundVelocity
activeBackground2.position.y += delta*backgroundVelocity
//Detect bounds surpass
if activeBackground1.position.y > activeBackground1.size.height + screen.height/2 {
lastSky = (lastSky + 1)%4
sky1 = SKTexture(imageNamed: "sky" + String(lastSky))
activeBackground1.texture = sky1
//Reposition: background1 new position is equal to minus the entire height of
//background2 + its y size.
activeBackground1.position.y = -abs(activeBackground2.size.height-activeBackground2.position.y)
}
if activeBackground2.position.y > activeBackground2.size.height + screen.height/2 {
lastSky = (lastSky + 1)%4
sky1 = SKTexture(imageNamed: "sky" + String(lastSky))
activeBackground2.texture = sky1
activeBackground2.position.y = -abs(activeBackground1.size.height-activeBackground1.position.y)
}
}
更新算法工作正常,但是当需要重新定位两个背景之一时,似乎从一个背景到另一个背景有大约 10.0 CGFloat
的偏移量。我做错了什么?
编辑:事实证明,错误位于我的图像中,它显示了一些空白行,因此产生了可视化故障。所以我的代码可以完美运行。
我做了测试,你很可能应该使用类似的东西:
activeBackground2.position.y = activeBackground1.size.height + activeBackground1.position.y
而不是
activeBackground2.position.y = -abs(activeBackground1.size.height-activeBackground1.position.y)
我做了这个例子,它工作正常:https://github.com/Maetschl/SpriteKitExamples/tree/master/InfiniteBackground/InfiniteBackground 随意看用。
您的问题是导致舍入错误的浮点数学运算。我现在在phone所以我不能写代码,但你想要做的是让 1 个父 SKNode 来处理你的整个背景。
将您的背景条添加到父节点。 然后,您仅将移动操作放在父项上。
当每条条子离开筛网时,您拿起条子,将其移动到其他条子的末端。
这种跳跃应该始终用整数数学来完成,不留空洞。
基本上: 浮点移动数学在父节点上完成。
基于整数的瓦片跳跃在每个条子上完成。