我在xcode和spritekit中使用swift编程语言。我想让我的背景图像永远滚动。到目前为止我有这个代码,但是当我在模拟器中运行它时,它只经过一次并返回到灰色背景。如何获得它以使背景永远重复。我已经有了移动它的背景,我想让它永远重复。
class GameScene: SKScene, SKPhysicsContactDelegate {
var background = SKSpriteNode()
// Background
background = SKSpriteNode(imageNamed: "background")
background.position = CGPointMake(self.size.width/2, self.size.height/2)
addChild(background)
// Repeat Background Image Forever
moveForeverAction()
}
//loop background image infinitely
func moveForeverAction() {
let moveNode = SKAction.moveByX(0, y: background.size.height * 2.0 , duration: NSTimeInterval(0.01 * background.size.height * 2.0))
let resetPosition = SKAction.moveByX(0, y: background.size.height * 2.0, duration: 0)
let moveNodeForever = SKAction.repeatActionForever(SKAction.sequence([moveNode, resetPosition]))
background.runAction(moveNodeForever)
}
答案 0 :(得分:1)
这是因为您正在添加相同的背景。 在循环中添加另一个bacground声明:
for var i:CGFloat=0; i < 3; ++i {
**background = SKSpriteNode(imageNamed: "background")**
background.position = CGPointMake(background.size.width / 2, i * background.size.height )
background.runAction(moveNodeForever)
}
答案 1 :(得分:0)
你有3次runAction()。例如:
对于var i:CGFloat = 0;我&lt; 3; ++ i {
background.position = CGPointMake(background.size.width / 2, i * background.size.height )
background.runAction(moveNodeForever)
}