If we want to build a procedurally generated level for a side-scrolling platform game, then the first step is to get it to scroll to the side.
Crazy notion, right?
This is a pretty easy task when it comes right down to it, and takes only a few lines of code.
To accomplish our task, we’re going to take full advantage of the way Sprite Kit handles it’s scenes. Every object in Spite Kit is basically a sub-class of the SKNode class, and every SKNode has a parent and a bunch of children, forming a hierarchy or tree.
So what we’ll do in order to get our platforms to scroll is to create a new class: an ScrollingNode that is a subclass of an SKNode.
We’ll have to manage some way of determining how fast we want our platforms to scroll right? So we’ll have to add a property to track that.
Since the only functionality that we really want from our ScrollingNode is to scroll (it is in the name after all), we’ll only have a single method, which over writes the update method that’s already present.
[self.children enumerateObjectsUsingBlock:^(SKSpriteNode * child, NSUInteger idx, BOOL *stop)
child.position = CGPointMake(child.position.x-self.scrollingSpeed, child.position.y);
This method is what takes advantage of the SKNode’s design.
The only thing this method does is to enumerate through every child of our ScrollingNode, and move them by the scrollingSpeed that we’ve already added.
And that’s it.
…I told you it would be simple…