我的背景用完了怎么替换?
How do I replace my background when it runs out?
var replacebg = SKAction.moveToY(bgTexture.size().height , duration: 0)
var replacebgForever = SKAction.repeatActionForever(replacebg)
bg.runAction(replacebgForever)
屏幕上的每一次触摸都会将背景向下移动 50 像素。我想在用完时使用相同的图像替换背景。我试过上面的代码,但它不起作用,而是停止了我所有的动画。有什么帮助吗?谢谢。
在SKScene
中的update
方法中,如果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
}
以上代码可以更加模块化。但我把它留给你。
var replacebg = SKAction.moveToY(bgTexture.size().height , duration: 0)
var replacebgForever = SKAction.repeatActionForever(replacebg)
bg.runAction(replacebgForever)
屏幕上的每一次触摸都会将背景向下移动 50 像素。我想在用完时使用相同的图像替换背景。我试过上面的代码,但它不起作用,而是停止了我所有的动画。有什么帮助吗?谢谢。
在SKScene
中的update
方法中,如果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
}
以上代码可以更加模块化。但我把它留给你。