简体   繁体   中英

Repeating Background with SpriteKit and Swift

Im playing around with swift and i would like to make a simple game using SpriteKit. In the background of the app is texture image that should repeat.

I mean like [img][img][img][img]

How can i do this?

In aditional I want to move it forever. But I already solved this part.

Here is my code:

 func generateWave(){

    var wave: SKSpriteNode = SKSpriteNode(imageNamed: "wave")

    var moveWave = SKAction.moveByX(-wave.size.width * 2.0, y: 0, duration: NSTimeInterval(0.02 * wave.size.width * 2.0))
    var resetWave = SKAction.moveByX(wave.size.width * 2.0, y: 0, duration: 0.0)
    var moveWaveForever = SKAction.repeatActionForever(SKAction.sequence([moveWave,resetWave]))

    wave.position = CGPoint(x:CGFloat(1),y:CGFloat(10))

    wave.runAction(moveWaveForever)

    self.addChild(wave)

}

Try using a for loop after making your background move. Like this:

#

    bgTexture = SKTexture(imageNamed: "wave")
    var movebg = SKAction.moveByX(-bgTexture.size().width, y: 0, duration: 9)
    var replacebg = SKAction.moveByX(bgTexture.size().width, y: 0, duration: 0)
    moveForever = SKAction.repeatActionForever(SKAction.sequence([movebg, replacebg]))
    for var i:CGFloat=0; i<3; i++ {

        wave = SKSpriteNode(texture: bgTexture)
        wave.position = CGPoint(x: bgTexture.size().width/2 + bgTexture.size().width * i, y: CGRectGetMidY(self.frame))
        wave.size.height = self.frame.height

        wave.runAction(moveForever)

        self.addChild(wave) 

} ##

I hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM