Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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