How do you adapt your nodes position to landscape & portrait
up vote
0
down vote
favorite
Im trying to make a game board that is universal and can be played on ipad/iphones either in portrait or landscape. The tiles looks fine in portrait but when i switch to landscape it gets cut off. How can i make sure the board will always be centered?
override func didMove(to view: SKView) {
let numRows = 7
let numCols = 7
var counter = 0
let squareSize = CGSize(width: view.bounds.width/7, height: view.bounds.width/7)
for row in 0..<numRows{
for col in 0..<numCols {
if counter == 49 {
break
}
let spriteNode = SKSpriteNode(imageNamed: "piece(counter)")
spriteNode.size = squareSize
spriteNode.position = CGPoint(x: 0-(frame.width*0.43)+(CGFloat(col)*spriteNode.size.width), y: -CGFloat(row)*spriteNode.size.height + frame.height/3 )
addChild(spriteNode)
counter = counter+1
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
if let scene = SKScene(fileNamed: "GameScene") {
scene.scaleMode = .resizeFill
view.presentScene(scene)
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
It looks like this:


ios swift sprite-kit position orientation
add a comment |
up vote
0
down vote
favorite
Im trying to make a game board that is universal and can be played on ipad/iphones either in portrait or landscape. The tiles looks fine in portrait but when i switch to landscape it gets cut off. How can i make sure the board will always be centered?
override func didMove(to view: SKView) {
let numRows = 7
let numCols = 7
var counter = 0
let squareSize = CGSize(width: view.bounds.width/7, height: view.bounds.width/7)
for row in 0..<numRows{
for col in 0..<numCols {
if counter == 49 {
break
}
let spriteNode = SKSpriteNode(imageNamed: "piece(counter)")
spriteNode.size = squareSize
spriteNode.position = CGPoint(x: 0-(frame.width*0.43)+(CGFloat(col)*spriteNode.size.width), y: -CGFloat(row)*spriteNode.size.height + frame.height/3 )
addChild(spriteNode)
counter = counter+1
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
if let scene = SKScene(fileNamed: "GameScene") {
scene.scaleMode = .resizeFill
view.presentScene(scene)
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
It looks like this:


ios swift sprite-kit position orientation
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Im trying to make a game board that is universal and can be played on ipad/iphones either in portrait or landscape. The tiles looks fine in portrait but when i switch to landscape it gets cut off. How can i make sure the board will always be centered?
override func didMove(to view: SKView) {
let numRows = 7
let numCols = 7
var counter = 0
let squareSize = CGSize(width: view.bounds.width/7, height: view.bounds.width/7)
for row in 0..<numRows{
for col in 0..<numCols {
if counter == 49 {
break
}
let spriteNode = SKSpriteNode(imageNamed: "piece(counter)")
spriteNode.size = squareSize
spriteNode.position = CGPoint(x: 0-(frame.width*0.43)+(CGFloat(col)*spriteNode.size.width), y: -CGFloat(row)*spriteNode.size.height + frame.height/3 )
addChild(spriteNode)
counter = counter+1
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
if let scene = SKScene(fileNamed: "GameScene") {
scene.scaleMode = .resizeFill
view.presentScene(scene)
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
It looks like this:


ios swift sprite-kit position orientation
Im trying to make a game board that is universal and can be played on ipad/iphones either in portrait or landscape. The tiles looks fine in portrait but when i switch to landscape it gets cut off. How can i make sure the board will always be centered?
override func didMove(to view: SKView) {
let numRows = 7
let numCols = 7
var counter = 0
let squareSize = CGSize(width: view.bounds.width/7, height: view.bounds.width/7)
for row in 0..<numRows{
for col in 0..<numCols {
if counter == 49 {
break
}
let spriteNode = SKSpriteNode(imageNamed: "piece(counter)")
spriteNode.size = squareSize
spriteNode.position = CGPoint(x: 0-(frame.width*0.43)+(CGFloat(col)*spriteNode.size.width), y: -CGFloat(row)*spriteNode.size.height + frame.height/3 )
addChild(spriteNode)
counter = counter+1
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
if let scene = SKScene(fileNamed: "GameScene") {
scene.scaleMode = .resizeFill
view.presentScene(scene)
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
It looks like this:


ios swift sprite-kit position orientation
ios swift sprite-kit position orientation
asked Nov 21 at 17:26
xxFlashxx
1168
1168
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
I would change the Scene scale mode from scene.scaleMode = .resizeFill to scene.scaleMode = .aspectFit. In a game the SKView frame size is not required and you can work within your own coordinate space (size property of the scene) e.g. set to 100 x 100. -> Interface Builder setting.
Don't relate anything on the view frame/bounds of the SKView. The code from your scene controller should work then as below, all related to the size of the scene.
override func didMove(to view: SKView) {
let numRows = 7
let numCols = 7
var counter = 0
let squareSize = CGSize(width: size.width/7, height: size.width/7)
for row in 0..<numRows{
for col in 0..<numCols {
if counter == 49 {
break
}
let spriteNode = SKSpriteNode(imageNamed: "piece(counter)")
spriteNode.size = squareSize
spriteNode.position = CGPoint(x: 0-(size.width*0.43)+(CGFloat(col)*spriteNode.size.width), y: -CGFloat(row)*spriteNode.size.height + size.height/3 )
addChild(spriteNode)
counter = counter+1
}
}
}
It adds a black bar around the screen which isn't exactly what i want.
– xxFlashxx
Nov 22 at 19:00
@xxFlashxx The black border which is visible in your screenshots as well ?. I explained you how to to use the SKScene coordinate system to center the Nodes in your scene.
– Marc T.
Nov 23 at 6:08
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I would change the Scene scale mode from scene.scaleMode = .resizeFill to scene.scaleMode = .aspectFit. In a game the SKView frame size is not required and you can work within your own coordinate space (size property of the scene) e.g. set to 100 x 100. -> Interface Builder setting.
Don't relate anything on the view frame/bounds of the SKView. The code from your scene controller should work then as below, all related to the size of the scene.
override func didMove(to view: SKView) {
let numRows = 7
let numCols = 7
var counter = 0
let squareSize = CGSize(width: size.width/7, height: size.width/7)
for row in 0..<numRows{
for col in 0..<numCols {
if counter == 49 {
break
}
let spriteNode = SKSpriteNode(imageNamed: "piece(counter)")
spriteNode.size = squareSize
spriteNode.position = CGPoint(x: 0-(size.width*0.43)+(CGFloat(col)*spriteNode.size.width), y: -CGFloat(row)*spriteNode.size.height + size.height/3 )
addChild(spriteNode)
counter = counter+1
}
}
}
It adds a black bar around the screen which isn't exactly what i want.
– xxFlashxx
Nov 22 at 19:00
@xxFlashxx The black border which is visible in your screenshots as well ?. I explained you how to to use the SKScene coordinate system to center the Nodes in your scene.
– Marc T.
Nov 23 at 6:08
add a comment |
up vote
0
down vote
I would change the Scene scale mode from scene.scaleMode = .resizeFill to scene.scaleMode = .aspectFit. In a game the SKView frame size is not required and you can work within your own coordinate space (size property of the scene) e.g. set to 100 x 100. -> Interface Builder setting.
Don't relate anything on the view frame/bounds of the SKView. The code from your scene controller should work then as below, all related to the size of the scene.
override func didMove(to view: SKView) {
let numRows = 7
let numCols = 7
var counter = 0
let squareSize = CGSize(width: size.width/7, height: size.width/7)
for row in 0..<numRows{
for col in 0..<numCols {
if counter == 49 {
break
}
let spriteNode = SKSpriteNode(imageNamed: "piece(counter)")
spriteNode.size = squareSize
spriteNode.position = CGPoint(x: 0-(size.width*0.43)+(CGFloat(col)*spriteNode.size.width), y: -CGFloat(row)*spriteNode.size.height + size.height/3 )
addChild(spriteNode)
counter = counter+1
}
}
}
It adds a black bar around the screen which isn't exactly what i want.
– xxFlashxx
Nov 22 at 19:00
@xxFlashxx The black border which is visible in your screenshots as well ?. I explained you how to to use the SKScene coordinate system to center the Nodes in your scene.
– Marc T.
Nov 23 at 6:08
add a comment |
up vote
0
down vote
up vote
0
down vote
I would change the Scene scale mode from scene.scaleMode = .resizeFill to scene.scaleMode = .aspectFit. In a game the SKView frame size is not required and you can work within your own coordinate space (size property of the scene) e.g. set to 100 x 100. -> Interface Builder setting.
Don't relate anything on the view frame/bounds of the SKView. The code from your scene controller should work then as below, all related to the size of the scene.
override func didMove(to view: SKView) {
let numRows = 7
let numCols = 7
var counter = 0
let squareSize = CGSize(width: size.width/7, height: size.width/7)
for row in 0..<numRows{
for col in 0..<numCols {
if counter == 49 {
break
}
let spriteNode = SKSpriteNode(imageNamed: "piece(counter)")
spriteNode.size = squareSize
spriteNode.position = CGPoint(x: 0-(size.width*0.43)+(CGFloat(col)*spriteNode.size.width), y: -CGFloat(row)*spriteNode.size.height + size.height/3 )
addChild(spriteNode)
counter = counter+1
}
}
}
I would change the Scene scale mode from scene.scaleMode = .resizeFill to scene.scaleMode = .aspectFit. In a game the SKView frame size is not required and you can work within your own coordinate space (size property of the scene) e.g. set to 100 x 100. -> Interface Builder setting.
Don't relate anything on the view frame/bounds of the SKView. The code from your scene controller should work then as below, all related to the size of the scene.
override func didMove(to view: SKView) {
let numRows = 7
let numCols = 7
var counter = 0
let squareSize = CGSize(width: size.width/7, height: size.width/7)
for row in 0..<numRows{
for col in 0..<numCols {
if counter == 49 {
break
}
let spriteNode = SKSpriteNode(imageNamed: "piece(counter)")
spriteNode.size = squareSize
spriteNode.position = CGPoint(x: 0-(size.width*0.43)+(CGFloat(col)*spriteNode.size.width), y: -CGFloat(row)*spriteNode.size.height + size.height/3 )
addChild(spriteNode)
counter = counter+1
}
}
}
answered Nov 21 at 19:21
Marc T.
962514
962514
It adds a black bar around the screen which isn't exactly what i want.
– xxFlashxx
Nov 22 at 19:00
@xxFlashxx The black border which is visible in your screenshots as well ?. I explained you how to to use the SKScene coordinate system to center the Nodes in your scene.
– Marc T.
Nov 23 at 6:08
add a comment |
It adds a black bar around the screen which isn't exactly what i want.
– xxFlashxx
Nov 22 at 19:00
@xxFlashxx The black border which is visible in your screenshots as well ?. I explained you how to to use the SKScene coordinate system to center the Nodes in your scene.
– Marc T.
Nov 23 at 6:08
It adds a black bar around the screen which isn't exactly what i want.
– xxFlashxx
Nov 22 at 19:00
It adds a black bar around the screen which isn't exactly what i want.
– xxFlashxx
Nov 22 at 19:00
@xxFlashxx The black border which is visible in your screenshots as well ?. I explained you how to to use the SKScene coordinate system to center the Nodes in your scene.
– Marc T.
Nov 23 at 6:08
@xxFlashxx The black border which is visible in your screenshots as well ?. I explained you how to to use the SKScene coordinate system to center the Nodes in your scene.
– Marc T.
Nov 23 at 6:08
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417581%2fhow-do-you-adapt-your-nodes-position-to-landscape-portrait%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown