Scrolling Parallax SpriteKit

I found a tutorial on scrolling parallax in spritekit using objective-C, although I tried to quickly port it to fast, but very little.

Scrolling Parallax

Does anyone have any other lessons or scroll scrolling methods in swift.

+4
source share
1 answer

This is a SUPER easy way to start parallax background. WITH SCACKS! I hope this helps you understand the basics before moving on to a more complex but more efficient way of coding this. So I'll start with code that moves the background, and then try to duplicate the code for the foreground or the objects you want to fit into your scene.

//declare ground picture. If Your putting this image over the top of another image (use a png file).
    var groundImage = SKTexture(imageNamed: "background.jpg")

    //make your SKActions that will move the image across the screen. this one goes from right to left.
    var moveBackground = SKAction.moveByX(-groundImage.size().width, y: 0, duration: NSTimeInterval(0.01 * groundImage.size().width))

    //This resets the image to begin again on the right side.
    var resetBackGround = SKAction.moveByX(groundImage.size().width, y: 0, duration: 0.0)

    //this moves the image run forever and put the action in the correct sequence.
    var moveBackgoundForever = SKAction.repeatActionForever(SKAction.sequence([moveBackground, resetBackGround]))

    //then run a for loop to make the images line up end to end.
    for var i:CGFloat = 0; i<2 + self.frame.size.width / (groundImage.size().width); ++i {
        var sprite = SKSpriteNode(texture: groundImage)
        sprite.position = CGPointMake(i * sprite.size.width, sprite.size.height / 2)
        sprite.runAction(moveBackgoundForever)
        self.addChild(sprite)
    }
}
//once this is done repeat for a forground or other items but them run at a different speed.

/* , . , , . . , , , , , . , . , : ObjectiveC, Swift. !

+4

All Articles