SpriteKit Camera position correction in iOS10?

2019-05-30 01:54发布

问题:

When converting my App, which has a SpriteKit scene in it, to iOS 10 I have had to convert my scene's original camera position from:

cameraNode.position = CGPoint(x: self.size.width/2, y: self.size.height/2)

to account for iOS10 with:

if #available(iOS 10, *) {
    cameraNode.position = CGPoint(x: self.size.width/2, y: (self.size.height + voidSize)/2)
}
else {
    cameraNode.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
}

The iOS10 version actually makes more sense as I do have part of the scene off-screen (where sprites are first created before being 'revealed' to the user) and 'voidSize' represents this. So having to add an offset to account for the off screen portion makes sense.

The question is, what has changed in SpriteKit in iOS 10 to make me correct my camera position like this that was not there before?

The app now works fine, on both iOS 9 and iOS 10 - just curious about what has caused this.

回答1:

There was a bug with SKCameraNode where it would not centre correctly. It now works correctly with ios 10 so DO NOT use the ios 9 check, just use the new setting which you said makes a lot more sense anyway.

My games on the app store that have not been updated for ios 10 yet now look wrong. So this must be a general fix and not an iOS version specific fix.

I even made a bug report about this which have apple responded to and confirmed to be fixed (27171076)

The main gist was this

Summary: Using the default SpriteKit template with SKCameraNode and putting it right in the centre of the screen cause the camera to be offset on iPhones. On iPads it is centred as expected.

Steps to Reproduce: - Create new sprite kit default template - Add SKCamera Node and Centre in middle of screen (in code or using visual level editor)

Expected Results: That camera is centred on iPhone as it does on iPads.

Actual Results: Camera is offCenter on iPhones. In portrait HelloWorld labels is shifted to the left and in landscape it is shifted to the bottom."