(Really) Flexible positioning


I originally introduced the idea of normalised positioning into cocos2d, and it quickly got very popular. The X-men implemented it, and -objc of course implemented it.

The -objc implementation hase some flaws though. Maintaining a positionType property is not a good way, because you will have to check it’s state, before the position property makes sense. This easily gets very complicated, and adds a lot of code overhead.

In stead PureSwift takes a far more flexible and “much easier to expand on” approach.

All node coordinates in PureSwift, are in view coordinates. This is an extremely important fact to understand. The view coordinates defaults to screen resolution, meaning that if you are programming for an iPhone5, the coordinate system in which the node is placed, is always 568 * 320. While you can change the viewport as explained in The Magic Layer, you can not change the fact, that node coordinates are in view port coordinates.

In stead PureSwift adds categories, to expand on positioning. Let me give you an example. In previous versions, normalised positioning would look something like

myNode.positionType = CCPositionTypeNormalised

myNode.position = SomePointConstructor(x: 0.5, y:0.5)

In PureSwift, this will look like

myNode.position = parent.positionWithNormalisedPosition(x: 0.5, y:0.5)

The difference is of course, that the node itself now doesn’t have to maintain position types, which basically removes +90% of all positioning code. Apart from that, it gives unprecedented flexibility. Imagine this line of code.

secondButton.position = firstButton.positionWithNormalisedPosition(x: 1.1, y:0.0)

This will place the second button to the right of the first button, no matter where first button is.

This approach, plus the flexibility of categories, will allow for a lot of convenience methods, like upperLeft and lowerRight, to return relative positions. Apart from that, PureSwift will add a placement category, which similar to KVO in ObjC, will maintain positions, even if other nodes changes position or content size.

If you think this sounds interesting, pleas consider supporting the KickStarter Project