[英]Endless scrolling (repeating) background in Spritekit game - Swift
[英]How to make a vertical endless scrolling background in SpriteKit Swift 4
我需要为我的游戏制作一个从上到下无限滚动的背景。 这就是我现在所拥有的,但是背景是最底层的,但永远不会回到最上层。 这就是我现在所拥有的。 我正在xcode 9上用swift 4编写。
import SpriteKit
class GameScene: SKScene {
var ground = SKSpriteNode()
override func didMove(to view: SKView) {
createGrounds()
}
override func update(_ currentTime: TimeInterval) {
moveGrounds()
}
func createGrounds() {
for i in 0 ... 3 {
let ground = SKSpriteNode(imageNamed: "backGround")
ground.name = "Ground"
ground.size = CGSize(width: (self.scene?.size.width)!, height: ((self.scene?.size.height)! * 2))
ground.anchorPoint = CGPoint(x: 0.5, y:0.5)
ground.position = CGPoint(x: (CGFloat(i) * ground.size.height), y: (self.frame.size.height / 2))
self.addChild(ground)
print(i)
}
}
func moveGrounds() {
self.enumerateChildNodes(withName: "Ground", using: ({
(node, error) in
node.position.y -= 7
if node.position.y > ((self.scene?.size.height))! {
node.position.y += (self.scene?.size.height)! / 3
}
}))
}
}
首先,背景位于高度(self.frame.size.height / 2)
,您将y
每次减7,因此y
始终小于(self.frame.size.height / 2)
,怎么办您认为条件node.position.y > ((self.scene?.size.height))
可能是正确的吗?
您应该在小于0或类似的值时重置y
(取决于您管理anchor
和position
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.