Handling SpriteKit node positions

When SpriteKit was announced one of the features which really got my excited was the high compatibility between iOS and OSX. The demo project displayed at WWDC and then made available from the dev centre later even had a project which contained both iOS and Mac OSX versions.

One thing that SpriteKit seems to be lacking though is a universal control for positioning assets. While this isn't exactly hard to create there are an awful lot of ways to go about creating it and exploring which is the best is a long process.

Things to consider:

  • You will need to predicate a lot of the code to check if the compiler is compiling in OSX or iOS.
  • For the tool to be useful you will need to to be able to scale up and down screen co-ordinates and / or screen percentages, take in to account landscape / portrait orientation AND allow for fine tuning with offsets.
  • Don't forget to allow offsets for 4 inch screens.
  • Handle scaling, shuffling, etc on Mac OSX.
  • Do you want objects that are resized to keep their aspect ratio.
  • Varying anchor points.
  • Need to account for the future, such as error handling for unknown screen sizes, allowing room for changes and improvements in SpriteKit.

These are all very important and intertwined, so why even make a helper class to handle them? Well, there would be an awful lot of space taken up in our app by code if we re-wrote or copied and pasted every time we wanted to position or size an object.

It's also handy to be able to update these methods from the one place should code change in the future rather than digging through every single point you positioned an object.

The helper class I've been working on returns a CGPoint or CGSize depending on the parameters you pass in to the method. The co-ordinates you pass in are based on 3.5 inch iPhone screens and they are scaled based on the current screen size and fine tuning offsets.

The class isn't perfect and needs a lot of refinement and testing but I plan to release it on GitHUB after my first app using it is released in January. Any advice or points I may have missed would be most appreciated while I test this helper class out.