Spritekit游戏中无尽的滚动(重复)背景 - Swift [英] Endless scrolling (repeating) background in Spritekit game - Swift
问题描述
我想为我的spritekit游戏创建一个无休止的滚动背景,iT应该包含一两个图像,它们会重复自己?我发现这些一个和两个示例,但它们都在obj中。 C.
I want to Create a endless scrolling background for my spritekit game, iT should consist of one or two images probably, which repeat themselves? I found these one and two examples, but they are in obj. C.
我不知道如何在Swift中实现这一目标。是否可以手动设置速度?
I have no idea how I can achieve this in Swift. And is it possible to set the speed manually?
Ps:我没有转换obj的技巧。 C进入swift(新手到Xcode dev。)
Ps: I don't have the skill to convert obj. C into swift (newbie to Xcode dev.)
推荐答案
我知道这是游戏的后期,但我发现怎么做这也是水平的!
I know this is late to the game, but I found how to do this horizontally as well!
从Egghead的代码开始(优秀的工作!)我修改了一些东西:
Starting off with Egghead's code (Excellent work!) I modified some things:
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)
}
这篇关于Spritekit游戏中无尽的滚动(重复)背景 - Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!