Reputation: 4506
I'm extremely new to SpriteKit, and have been following some great tutorials by someone online. They all do one thing which I think might be a red flag, but wanted to ask the community about.
Prerequisite: all his demos are done using iPad (it's set in the app settings to only target iPad, so no intention of iPhone).
So what i'm seeing is the developer creates labels and graphics by using hard-coded points.
For example to make the text "game over" appear:
let gameOver = SKSpriteNode(imageNamed: "gameOver")
gameOver.position = CGPoint(x: 512, y: 384)
gameOver.zPosition = 1
addChild(gameOver)
So the thing about this code is it's hard-coded to be at a specific point. So this means that in theory, it's going to be slightly wrong on the iPad mini vs. the iPad pro, correct (since he intended this to be on the iPad air)? Or do the devices translate where that should be every time?
I feel like you'd want to do some fancy magic like calculate the width/height of the screen and divide by some number to calculate the center, rather than specify points.
In fact, all of his code more or less uses hard-coded points for example look at this to render sprites:
for i in 0 ..< 5 { drawSpriteAt(CGPoint(x: 100 + (i * 170), y: 410)) }
for i in 0 ..< 4 { drawSpriteAt(CGPoint(x: 180 + (i * 170), y: 320)) }
for i in 0 ..< 5 { drawSpriteAt(CGPoint(x: 100 + (i * 170), y: 230)) }
Is this not a major problem if I were targeting iPad mini and iPad pro as well?
For some reason, however, I ran this on the simulator in iPad pro and it actually looked pretty good.
So my question, ultimately then, is:
Is this just fine to do it this way? Do the different iPads somehow magically work out what the point (512, 384) is such that that point is different on an iPad mini vs. iPad pro?
Thanks and apologies if this is a basic question, I just wanted to see that if I were only writing an app to target iPhones, or iPads (i'm doing both actually) that I would make two separate apps with hard-coded coordinates specific for one device.
For example i'd say "on all iPads, regardless, use point (512,384)" and on an iPhone i'd say "on all iPhones of all sizes, use point (300, 150) or something like that.
Thanks so much for your help!
Upvotes: 2
Views: 196
Reputation: 11113
Sprite Kit is designed to have a scaling system so you don't have to worry as much about coordinates. The coordinates don't represent pixel or point coordinates on the screen - but are coordinates in the game scene. Basically if you setup a scaling mode for you game, your coordinates will be taken care of for multiple screen sizes. (You would be correct in your assumption if you were just talking about iOS button/view coordinates.)
See the documentation here: https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Nodes/Nodes.html
After a scene is rendered, its contents are copied into the presenting view. If the view and the scene are the same size, then the content can be directly copied into the view. If the two differ, then the scene is scaled to fit in the view. The scaleMode property determines how the content is scaled.
When you design your game, you should decide on a strategy for handling the scene’s size and scaleMode properties. Here are the most common strategies:
- Instantiate the scene with a constant size and never change it.
- Pick a scaling mode that lets the view scale the scene’s content. This gives the scene a predictable coordinate system and frame. You can then base your art assets and gameplay logic on this coordinate system.
- Adjust the size of the scene in your game. Where necessary, adjust your game logic and art assets to match the scene’s size.
- Set the scaleMode property to SKSceneScaleModeResizeFill. SpriteKit automatically resizes the scene so that it always matches the view’s size. Where necessary, adjust your game logic and art assets to match the scene’s size.
Upvotes: 3