Learn how to create a Sprite Kit project and add the background.
This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3875-how-to-make-a-game-like-flappy-bird/lessons/2
Learn how to create a Sprite Kit project and add the background.
Hi Tammy,
I added the code in GameScene.swift according to your video, but the foreground image is not getting displayed. Can you take a quick look? I seem not to find the mistake.
When I try to print “playableStart” to the console, I get -96.0 as a value on a iPhone4S. Maybe this helps?
Additionally I must say, that at the point when I am adding the background, the image is scaled far beyond the actual screen size and uses the complete screen size, although I have the correct saleMode of .AspectFill.
Here is my code `
import SpriteKit
enum Layer: CGFloat {
case Background
case Foreground
}
class GameScene: SKScene {
let worldNode = SKNode()
var playableStart: CGFloat = 0
var playableHeight: CGFloat = 0
override func didMoveToView(view: SKView) {
/* Setup your scene here */
addChild(worldNode)
setupBackground()
setupForeground()
}
// MARK: Setup methods
func setupBackground() {
let background = SKSpriteNode(imageNamed: "Background")
background.anchorPoint = CGPoint(x: 0.5, y: 1.0)
background.position = CGPoint(x: size.width/2, y: size.height)
background.zPosition = Layer.Background.rawValue
worldNode.addChild(background)
playableStart = size.height - background.size.height
playableHeight = background.size.height
}
func setupForeground() {
let foreground = SKSpriteNode(imageNamed: "Ground")
foreground.anchorPoint = CGPoint(x: 0.0, y: 1.0)
foreground.position = CGPoint(x: 0, y: playableStart)
foreground.zPosition = Layer.Foreground.rawValue
worldNode.addChild(foreground)
print("go")
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
`
I found my mistake,… I did not calculate the scene bounds correctly in my GameViewController when setting up the scene. Sorry for that,…
Cheers,
Johannes