Skip to content

Instantly share code, notes, and snippets.

@laevandus
Created December 3, 2017 20:27
Show Gist options
  • Save laevandus/8a78fa35a2829945fcb7abe6c44e8dcb to your computer and use it in GitHub Desktop.
Save laevandus/8a78fa35a2829945fcb7abe6c44e8dcb to your computer and use it in GitHub Desktop.
Keeping SKNode at the edge of a screen when scene is scaled.
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