我想为我的spritekit游戏创建一个无尽的滚动背景,iT应该包含一两个图像,它们会重复自己?我找到了这些one和two示例,但它们都在obj中。 C.
我不知道如何在Swift中实现这一目标。是否可以手动设置速度?
Ps:我没有转换obj的技巧。 C进入swift(新手到Xcode dev。)
答案 0 :(得分:4)
我找到了一种方法来做到这一点,不知怎的,我设法转换this 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循环。但在我看来这更容易。
答案 1 :(得分:4)
我知道这场比赛已经晚了,但我也发现了如何横向完成这项工作!
从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)
}
答案 2 :(得分:3)
我为此做了一堂课。目前它是为Swift 5.0或4.2编写的,它支持顶部,底部,左侧和右侧方向。
检查一下,看看它是否对你有帮助,它被称为InfiniteScrollingBackground:https://github.com/ThiagoAM/InfiniteScrollingBackground
答案 3 :(得分:2)
你不需要这一切。
只需将此函数与您声明的变量以及您在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)
}
}
然后在你的更新方法中调用它。
当然,这不是最好的方式,因为当你有更多的循环时它可以读取两个图像。
答案 4 :(得分:2)
我想出的是:
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)
}
}
所以基本上,地形被放置在视图上方,然后它们开始向下移动,并在需要时被新的替换,以重复。
答案 5 :(得分:1)
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)
}
}
-UPDATED FOR SWIFT 3.1 -
这是我一直用它来使它适用于Y轴只需将X位置的所有额外东西切换到Y位置