Created
December 3, 2017 20:27
-
-
Save laevandus/8a78fa35a2829945fcb7abe6c44e8dcb to your computer and use it in GitHub Desktop.
Keeping SKNode at the edge of a screen when scene is scaled.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private var initialSize: CGSize = .zero | |
private var presentedSize: CGSize { return scene?.view?.bounds.size ?? size } | |
private var presentedScaleFactor: CGFloat { return initialSize.width / presentedSize.width } | |
override func sceneDidLoad() | |
{ | |
super.sceneDidLoad() | |
initialSize = size | |
} | |
func layoutNodes() | |
{ | |
let margin: CGFloat = 10 | |
if let topRight = childNode(withName: "topRight") as? SKSpriteNode | |
{ | |
topRight.position.x = presentedSize.width / 2.0 * presentedScaleFactor - topRight.size.width / 2.0 - margin | |
topRight.position.y = presentedSize.height / 2.0 * presentedScaleFactor - topRight.size.height / 2.0 - margin | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment