Game Development Reference
In-Depth Information
Looking
at
SKSpriteNode
Coordinates
and Anchor Points
So far, I have talked about how you create an SKScene object and how it is rendered. I
am now going to talk about how you position nodes in a scene. Let's begin by looking at
the scene's coordinate system. Before you begin, be sure to change the GameS-
cene.init() method back to the following:
init(size: CGSize) {
super.init(size: size)
backgroundColor = SKColor(red: 0.0, green: 0.0, blue:
0.0, alpha: 1.0)
// adding the background
backgroundNode = SKSpriteNode(imageNamed: "Background")
backgroundNode!.anchorPoint = CGPoint(x: 0.5, y: 0.0)
backgroundNode!.position = CGPoint(x: size.width / 2.0,
y: 0.0)
addChild(backgroundNode!)
// add the player
playerNode = SKSpriteNode(imageNamed: "Player")
playerNode!.position = CGPoint(x: size.width / 2.0, y:
80.0)
addChild(playerNode!)
}
Coordinates
When a scene is first initialized, its size property is set in its initializer, as you saw earli-
er. The size of the scene denotes the size of the visible portion of the scene. It does not
define the entire size of the game world. You can think of the size of the scene as the
viewport into the game world.
By default, an SKScene 's origin is placed in the bottom-left corner of the view present-
ing it. The coordinate representing this origin is (0, 0). Going back to the game from the
previous chapter, add this line of code to the GameScene.init() method immediately
following the call to the super.init() :
Search WWH ::




Custom Search