Getting Started with SpriteKit by Jorge Jordan

Getting Started with SpriteKit by Jorge Jordan

Author:Jorge Jordan [Jordan, Jorge]
Language: eng
Format: azw3, epub
Publisher: Packt Publishing
Published: 2016-01-25T05:00:00+00:00


Furthermore, let's declare some variables that will be useful for the development of the parallax effect. Add the following lines just after private var isEnemyCollisionDetected: Bool = false:

private var backgroundBottom: SKSpriteNode! private var backgroundTop: SKSpriteNode! private var treesBottom: SKSpriteNode! private var treesTop: SKSpriteNode!

To initialize these variables, we are going to call a new method. Add the following line at the end of didMoveToView:

self.initializeParallaxEffect()

Implement this with the following lines of code:

func initializeParallaxEffect() { // Initialize background layers backgroundBottom = SKSpriteNode(imageNamed: "background") backgroundBottom.anchorPoint = .zero backgroundBottom.zPosition = -1 // Copy the previous node into another backgroundTop = backgroundBottom.copy() as! SKSpriteNode // Set top layer position backgroundTop.position = CGPoint(x: backgroundBottom.position.x, y: backgroundBottom.position.y + backgroundBottom.size.height) // Initialize tree layers treesBottom = SKSpriteNode(imageNamed: "trees") treesBottom.zPosition = 4 treesBottom.position = CGPoint(x:(view!.bounds.size.width/2), y: view!.bounds.size.height/2) // Copy the previous node into another treesTop = treesBottom.copy() as! SKSpriteNode // Set top layer position treesTop.position = CGPoint(x: treesBottom.position.x, y: treesBottom.position.y + treesBottom.size.height) // Add background layers to the scene addChild(backgroundBottom) addChild(backgroundTop) // Add tree layers to the scene addChild(treesBottom) addChild(treesTop) }



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.