【发布时间】:2014-12-08 11:09:19
【问题描述】:
我想为我的 spritekit 游戏创建一个无限滚动的背景,它应该由一两个图像组成,它们会重复吗?我找到了这些 one 和 two 示例,但它们在 obj 中。 C.
我不知道如何在 Swift 中实现这一点。是否可以手动设置速度?
Ps:我没有转换obj的技能。 C 到 swift(Xcode 开发新手)
【问题讨论】:
标签: ios iphone xcode swift sprite-kit
我想为我的 spritekit 游戏创建一个无限滚动的背景,它应该由一两个图像组成,它们会重复吗?我找到了这些 one 和 two 示例,但它们在 obj 中。 C.
我不知道如何在 Swift 中实现这一点。是否可以手动设置速度?
Ps:我没有转换obj的技能。 C 到 swift(Xcode 开发新手)
【问题讨论】:
标签: ios iphone xcode swift sprite-kit
我知道这对游戏来说已经很晚了,但我也找到了横向的方法!
从 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)
}
【讨论】:
我找到了一种方法来做到这一点,不知何故我设法转换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)
并且在你添加的“override func update(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 循环。但这在我看来更容易。
【讨论】:
我为此开设了一个课程。目前它是为 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 位置
【讨论】: