[英]Endless scrolling (repeating) background in Spritekit game - Swift
我找到了一种方法来做到这一点,不知怎的,我设法转换这个 obj。 C迅速
您必须公开声明这两个节点
let background1 = SKSpriteNode(imageNamed: "bg1")
let background2 = SKSpriteNode(imageNamed: "bg2")
在“didMoveToView”方法中
background1.anchorPoint = CGPointZero
background1.position = CGPointMake(0, 0)
background1.zPosition = -15
self.addChild(background1)
background2.anchorPoint = CGPointZero
background2.position = CGPointMake(0, background1.size.height - 1)
background2.zPosition = -15
self.addChild(background2)
并在您添加的“覆盖功能更新(currentTime:CFTimeInterval)”方法中
background1.position = CGPointMake(background1.position.x, background1.position.y - 2)
background2.position = CGPointMake(background2.position.x, background2.position.y - 2)
if(background1.position.y < -background1.size.height)
{
background1.position = CGPointMake(background2.position.x, background1.position.y + background2.size.height )
}
if(background2.position.y < -background2.size.height)
{
background2.position = CGPointMake(background1.position.x, background2.position.y + background1.size.height)
}
我不知道这是否是最有效的方式。 其他问题提到了For循环。 但在我看来这更容易。
我知道这场比赛已经晚了,但我也发现了如何横向进行!
从Egghead的代码开始(优秀的工作!)我修改了一些东西:
let background1 = SKSpriteNode(imageNamed: "Street_Example")
let background2 = SKSpriteNode(imageNamed: "Street_Example")
也:
background1.position = CGPoint(x: frame.size.width / 2, y:frame.size.height / 2)
background1.size = CGSize(width: frame.width, height: frame.height)
background1.anchorPoint = CGPointZero
background1.position = CGPointMake(0, 0)
background1.zPosition = -15
self.addChild(background1)
background2.size = CGSize(width: frame.width, height: frame.height)
background2.anchorPoint = CGPointZero
background2.position = CGPointMake(background1.size.width - 1,0)
background2.zPosition = -15
self.addChild(background2)
并更新背景的位置:
background1.position = CGPointMake(background1.position.x-2, background1.position.y)
background2.position = CGPointMake(background2.position.x-2, background2.position.y)
if(background1.position.x < -background1.size.width)
{
background1.position = CGPointMake(background1.position.x + background2.size.width , background2.position.y)
}
if(background2.position.x < -background2.size.width)
{
background2.position = CGPointMake(background2.position.x + background1.size.height, background1.position.y)
}
我为此做了一堂课。 目前它是为Swift 5.0或4.2编写的,它支持顶部,底部,左侧和右侧方向。
看看它是否有帮助,它叫做InfiniteScrollingBackground: https : //github.com/ThiagoAM/InfiniteScrollingBackground
你不需要那一切。
只需将此函数与您声明的变量以及您在didMoveToView中使用的属性一起使用。
func backgroudScrollUpdate(){
background1.position = CGPointMake(background1.position.x, background1.position.y - 1)
background2.position = CGPointMake(background1.position.x, background2.position.y - 1)
if background1.position.y == -UIScreen.mainScreen().bounds.height{
background1.position = CGPointMake(0, 0)
background2.position = CGPointMake(0, 0 + UIScreen.mainScreen().bounds.height)
}
}
然后在您的更新方法中调用它。
当然,它不是最好的方式,因为它有两个图像可读,当你有更多的循环。
在这里我提出了:
func terrain() -> SKNode {
let color = UIColor.init(red: randomColorChannel(), green: randomColorChannel(), blue: randomColorChannel(), alpha: 1.0)
let scale: CGFloat = 1.0// UIScreen.mainScreen().scale
let size = CGSizeMake(frame.size.width * scale, frame.size.height * scale)
let node = SKSpriteNode.init(color: color, size: size)
node.anchorPoint = CGPointZero
node.position = CGPointMake(0, UIScreen.mainScreen().bounds.size.height)
return node
}
在更新中执行此操作:
override func update(currentTime: NSTimeInterval) {
var newPosition1 = terrain1.position
var newPosition2 = terrain2.position
newPosition1.y -= terrainSpeed
if newPosition1.y < 0 {
newPosition2.y -= terrainSpeed
}
terrain1.position = newPosition1
terrain2.position = newPosition2
if terrain1.position.y <= -UIScreen.mainScreen().bounds.size.height {
removeChildrenInArray([terrain1])
terrain1 = terrain()
addChild(terrain1)
}
if terrain2.position.y <= -UIScreen.mainScreen().bounds.size.height {
removeChildrenInArray([terrain2])
terrain2 = terrain()
addChild(terrain2)
}
}
所以基本上,地形被放置在视图上方,然后它们开始向下移动,并在需要时被新的替换,以重复。
func backgroudScrollUpdate(){
BG .position = CGPoint(x: BG.position.x - 5, y: BG.position.y)
BG2.position = CGPoint(x: BG2.position.x - 5, y: BG2.position.y)
if BG2.position.x <= -self.frame.size.width {
BG.position = CGPoint(x: self.frame.size.width, y: 0)
}
if BG.position.x <= -self.frame.size.width {
BG2.position = CGPoint(x: self.frame.size.width, y: 0)
}
}
- 适用于SWIFT 3.1-
这是我一直用它来使它适用于Y轴只需将X位置的所有额外东西切换到Y位置
var backgroundImageWidth: CGFloat = 3000
override func didMove(to view: SKView) {
self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
for i in 0...2 {
let backgroundImage = SKSpriteNode(imageNamed: "BackgroundImage")
backgroundImage.name = "back"
backgroundImage.size = CGSize(width: backgroundImageWidth, height: 1000)
backgroundImage.anchorPoint = CGPoint(x: 0.5, y: 0.5)
backgroundImage.position = CGPoint(x: CGFloat(i)*backgroundImage.size.width, y:0)
self.addChild(backgroundImage)
}
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
self.enumerateChildNodes(withName: "back", using: ({
(node, error) in
node.position.x -= 2
if node.position.x < -(2 * self.backgroundImageWidth) {
node.position.x += 3 * self.backgroundImageWidth
}
}))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.