SpriteKit: Some (easy?) beginner questions

I am new to SpriteKit and Swift, only developed one little Swift App for iOS a month ago. I want to learn more, so here I am doing a little 2D game in Swift / SpriteKit. This is a collection of little questions I have which I did not find any answers to after searching a bit.


1. If I set a scene in the editor to 1024x768, how can I then adapt that size to the user's screen size? There is no option in the editor to simply say "width: 100%, height:100%", like in HTML. Let's say I am setting up my scene in 1024x768, decorating stuff and placing my SKSpriteNodes, some of the enviroment is 16x16, some is 128x128. I guess i need to somehow then fit the scene on each users device in my code resulting in downscaled or upscaled images, right? What's the best practice here? How to? I want something easy like:


scene.scaleSizeTo(userScreen.size)


2. If i put a SKSpriteNode in the Scene Editor, I can then alter it's physics. I simply want the Sprite to work as environment in the scene, which a player can not cross => can contact / collide with. I used only "pinned" and dechecked "dynamic", "allows Rotation" and "affected by gravity", since i want it to simply stay there and "block" space. Then i gave it a category bit mask like "1000" and a contact bit mask like "10", because my bitmask enum looks like that:


static let enemy: UInt32 = 0b00000000000000000000000000000001

static let bullet: UInt32 = 0b00000000000000000000000000000010

static let hero: UInt32 = 0b00000000000000000000000000000100

static let environment: UInt32 = 0b00000000000000000000000000001000

static let powerup: UInt32 = 0b00000000000000000000000000010000

static let enemyBullet: UInt32 = 0b00000000000000000000000000100000

However my hero can still cross the environment block AND there is no SKPhysicsContact thrown. What am i doing wrong here?

3. How do I "fly" through my level as a player? I managed to get the acceleration to work and move the ship on screen to different positions, but i don't know how to let the level "flow". The level should simply scroll as the player plays so that the player will at some point reach the end (assuming my scene is like 10000y high).

I will update this thread, if I have more questions. I hope this forum is the correct place to ask this supposedly easy beginner stuff.

Thanks a lot from a beginner in iOS / Swift / SpriteKit. I only have a lot experience in Java / PHP.

Hi Mav3N.

first: you can change scene sclaeMode.

scene.size = skView.bounds.size

scene.scaleMode = .AspectFill

second: use SKPhysicsBody(edgeFromPoint: <#CGPoint#>, toPoint: <#CGPoint#>) to create such walls.

third: Apple documentation about SpriteKit:

Advanced Scene Processing

Example: Centering the Scene on a Node

For the third point I would suggest to use SKCameraNode. With this node you can easily manage the position/scale/rotation of your camera using this special node.

You can find more information about it in the WWDC 2015 SpriteKit Videos(What's new in SpriteKit)

Hi,


thanks for your answers.I managed to get the first two Points working basically. The third point is somehow complicated.


I added a camera, set it as the scene's camera, initially set it to the players position and then made it follow the player through code in the update() function. Doesn't really work however. This is my code:


let theCamera = SKCameraNode() 
class GameScene: SKScene, SKPhysicsContactDelegate { 
     let player: Hero() 

     override func didMoveToView(view: SKView) { 
          super.didMoveToView(view) 
          player.setPosition(view.bounds.size.width / 2, y: view.bounds.size.height / 5)
          self.camera = theCamera 
          self.camera?.position = player.position 
     }
}


And my update function is:


player.position.y += 1
var offsetY = CGFloat(0)
       
if player.position.y < (self.view?.bounds.size.height)! / 5 {
     offsetY = (self.view?.bounds.size.height)! / 5 - player.position.y
}
self.camera?.position = CGPointMake(CGRectGetMidX((self.view?.bounds)!), player.position.y + offsetY)


What happens is, that my player spawns at 160/96 for a 320/480 device and my camera is set to the same position, as some debug print() shows. However visually the "scrolling" effect starts at the middle of the y-scene. So when i start my app i am visually at y=1000 for a y=2000 scene and start scrolling from there, instead of watching the player even though my camera and player are at y=96.


What am i doing wrong?


Thanks!

One problem I see in your code is this line:

player.position.y += 1

I think you need to set the position direcly like that:

player.position = CGPointMake(player.position.x, player.position.y + 1)

Because as far as I know your code does not call the setter of the position property.


Besides that : Did you add the camera node to your scene ? You need to do that to get it working.

SpriteKit: Some (easy?) beginner questions
 
 
Q