var replacebg = SKAction.moveToY(bgTexture.size().height , duration: 0)
var replacebgForever = SKAction.repeatActionForever(replacebg)
bg.runAction(replacebgForever)
屏幕上的每次触摸都会将背景向下移动50像素。我希望在用完时使用相同的图像替换背景。我已经尝试了上面的代码,但它不起作用,而是停止我的所有动画。有帮助吗?谢谢。
答案 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
}
上述代码可以更加模块化。但我把它留给你。