Add the Foreground

You’ve seen how to add an image set and get an SKSpriteNode object initialized, positioned, and added to the scene. You did this with the background. It’s now time to apply that same knowledge and get the foreground added to your scene.

Add the Foreground Images

Before you can add a sprite node with a corresponding image file, you first need to add the images to the project as you did with the background.

In the Project Navigator, select the Assets.xcassets asset catalog.

Once again, launch a Finder window and navigate to the resources folder included with the code resources for this chapter. This time, select the three foreground images:

Drag the foreground images into the Outline View of the Assets.xcassets asset catalog.

Add the Foreground Node

Your next step is to add the code to initialize the sprite node.

Open the GameScene.swift file. In the didMove(to:) method, below the code block that sets up the background node, add the following code:

 // Set up foreground
 let​ foreground = ​SKSpriteNode​(imageNamed: ​"foreground_1"​)
 foreground.anchorPoint = ​CGPoint​(x: 0, y: 0)
 foreground.position = ​CGPoint​(x: 0, y: 0)
 addChild​(foreground)

Build and run the project. You now have a foreground on which your player can stand.

images/CreatingScenesWithSpritesAndNodes/spritekit-build-07.png
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset