如何在用完后替换背景?

时间:2015-01-12 01:59:09

标签: ios swift background sprite-kit

var replacebg = SKAction.moveToY(bgTexture.size().height , duration: 0)
    var replacebgForever = SKAction.repeatActionForever(replacebg)

    bg.runAction(replacebgForever)

屏幕上的每次触摸都会将背景向下移动50像素。我希望在用完时使用相同的图像替换背景。我已经尝试了上面的代码,但它不起作用,而是停止我的所有动画。有帮助吗?谢谢。

1 个答案:

答案 0 :(得分:0)

update中的SKScene方法中,如果退出屏幕,您可以重置background

override func update(currentTime: CFTimeInterval) {
    if bg.position.y + bg.size.height/2 < 0
    {
        bg.position.y = self.frame.height + bg.size.height/2

    }
}

要创建环绕效果,请创建两个背景精灵节点,如下所示。这两个背景是一个在另一个之上。

background1 = SKSpriteNode(texture: bgTexture)
background1.position = CGPoint(x:CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
background1.size.height = self.frame.height

background2 = SKSpriteNode(texture: bgTexture)
background2.position = CGPoint(x:CGRectGetMidX(self.frame) , y: CGRectGetMidY(self.frame)+ self.frame.size.height)
background2.size.height = self.frame.height

self.addChild(background1)
self.addChild(background2)

然后在touchesBegan函数

if background1.position.y + background1.size.height/2 < 50
{
    let diff =  background1.position.y + background1.size.height/2 - 50
    background1.position.y = self.frame.height + background1.size.height/2 + diff
}
else
{
     background1.position.y -= 50
}
if background2.position.y + background2.size.height/2 < 50
{
    let diff =  background2.position.y + background2.size.height/2 - 50
    background2.position.y = self.frame.height + background2.size.height/2 + diff
}
else
{
    background2.position.y -= 50
}

上述代码可以更加模块化。但我把它留给你。