How do you adapt your nodes position to landscape & portrait
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 |
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 |
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 '18 at 17:26
xxFlashxx
1168
1168
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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 '18 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 '18 at 6:08
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 '18 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 '18 at 6:08
add a comment |
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 '18 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 '18 at 6:08
add a comment |
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 '18 at 19:21
Marc T.
1,002614
1,002614
It adds a black bar around the screen which isn't exactly what i want.
– xxFlashxx
Nov 22 '18 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 '18 at 6:08
add a comment |
It adds a black bar around the screen which isn't exactly what i want.
– xxFlashxx
Nov 22 '18 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 '18 at 6:08
It adds a black bar around the screen which isn't exactly what i want.
– xxFlashxx
Nov 22 '18 at 19:00
It adds a black bar around the screen which isn't exactly what i want.
– xxFlashxx
Nov 22 '18 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 '18 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 '18 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