如何在spritekit中纵向模式滚动背景

时间:2016-06-06 19:10:48

标签: ios xcode swift sprite-kit

我希望在手机处于纵向模式时进行滚动背景我尝试了这些代码,但它在纵向模式下没有帮助我

let background1 = SKSpriteNode(imageNamed: "space")
let background2 = SKSpriteNode(imageNamed: "space")

在" didMoveToView"方法

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 个答案:

答案 0 :(得分:0)

我会说这个,X =(1,2),导致你的问题:

backgroundX.size = CGSize(width: frame.width, height: frame.height)

您将背景纹理的大小设置为场景框架的大小,覆盖从纹理源继承的原始大小,并且需要正确计算其新位置。

答案 1 :(得分:0)

您有一些错误,例如使用高度来计算background2的x位置。这应该让你朝着正确的方向前进......

didMoveToView

background1.size = CGSize(width: frame.width, height: frame.height)
background1.anchorPoint = CGPointZero
background1.position = CGPointZero
background1.zPosition = 0
self.addChild(background1)

background2.size = CGSize(width: frame.width, height: frame.height)
background2.anchorPoint = CGPointZero
background2.position = CGPoint(x: background2.size.width, y: 0)
background2.zPosition = 0
self.addChild(background2)

更新

background1.position = CGPoint(x: background1.position.x - 2, y: background1.position.y)
background2.position = CGPoint(x: background2.position.x - 2, y: background2.position.y)

if (background1.position.x <= -background1.size.width) {
   background1.position = CGPoint(x: background1.position.x + background1.size.width * 2, y: 0)
}
if (background2.position.x <= -background2.size.width) {
   background2.position = CGPoint(x: background2.position.x + background1.size.width * 2, y: 0)
}

在gameViewController中,在呈现场景之前,您可能还想添加

scene.size = view.bounds.size