循环的时间不正确,并且重置得太快。循环图像也被剪切掉,只显示其中的一小部分,而不是缝合到上一个的完整循环背景图像。
我曾尝试使用.position,.size和SKAction值,但是我似乎无法将其调整到可以正常工作的位置。
func scrollingBackground() {
let BGTexture = SKTexture(imageNamed: "Background")
for i in 0 ... 1 {
let background = SKSpriteNode(texture: BGTexture)
background.zPosition = -30
background.anchorPoint = CGPoint(x: 0, y: 0)
background.size = CGSize(width: (frame.size.width), height: (frame.size.height))
background.position = CGPoint(x: 0, y: -BGTexture.size().height + (BGTexture.size().height + (BGTexture.size().height * CGFloat(i))))
addChild(background)
let scrollUp = SKAction.moveBy(x: 0, y: BGTexture.size().height, duration: 20)
let scrollReset = SKAction.moveBy(x: 0, y: -BGTexture.size().height, duration: 0)
let scrollLoop = SKAction.sequence([scrollUp, scrollReset])
let scrollForever = SKAction.repeatForever(scrollLoop)
background.run(scrollForever)
}
我希望获得一个无缝无限的背景循环,该循环可以无限期运行,而不是当前存在的那种混乱的混乱状态。我可能会补充说,我的背景图片原本无法在设备上调整大小,但我想确保我的背景适合所有iPhone设备。
答案 0 :(得分:0)
您的第一个BG位于y = 0,您的第二个BG位于y =高度。我假设您位于锚点0.5 0.5上,这将使屏幕高度/ 2偏离屏幕。您的动作将BG 1精确地移至屏幕外的位置,然后碰撞回到0。BG2开始离开屏幕,继续移出屏幕,然后当其达到2 *高度时,重置为1 *高度,因此永远不会出现在屏幕上。屏幕。这将使底部显示黑色条,因为从未绘制BG2。将您的i
乘以负高度,这将导致BG2出现在底部。
func scrollingBackground() {
let BGTexture = SKTexture(imageNamed: "Background")
for i in 0 ... 1 {
let background = SKSpriteNode(texture: BGTexture)
background.zPosition = -30
background.anchorPoint = CGPoint(x: 0, y: 0)
background.size = CGSize(width: (frame.size.width), height: (frame.size.height))
background.position = CGPoint(x: 0, y: -BGTexture.size().height + (BGTexture.size().height + (-BGTexture.size().height * CGFloat(i))))
addChild(background)
let scrollUp = SKAction.moveBy(x: 0, y: BGTexture.size().height, duration: 20)
let scrollReset = SKAction.moveBy(x: 0, y: -BGTexture.size().height, duration: 0)
let scrollLoop = SKAction.sequence([scrollUp, scrollReset])
let scrollForever = SKAction.repeatForever(scrollLoop)
background.run(scrollForever)
}