Last time we built a quick class to get our scene moving, but that’s not particularly helpful until we have something to move. In this post, we’ll add a set of platforms to our scene.
This first step is to create a new class that sub-classes our ScrollingNode that we built in the last post.
@interface PlatformNode : ScrollingNode
This will ensure that whatever we add to this whatever we add to our PlatformNode will be able to scroll at whatever speed we decide, which was the point of our work in the last post.
Now that we have a class to get things started (or scrolled), we will build a way to keep track of a set of platforms.
One of the advantages of using a procedural level generator, is that it can lower our overhead when it comes to keeping a number of large map or level in system memory. So, as we build our platform side-scroller we have to keep in mind that we want to limit the total number of platforms that our program will need to keep track of at any given time.
To do this, we’ll build a platform buffer to limit the number of platforms we track. Change our PlatformNode.h file as shown below:
@property (nonatomic) NSUInteger platformBufferSize;
What we’ve done is to add:
- A Mutable Array that can store any number of objects. We’ll use this to store our Platform Nodes.
- An Integer to track how many Platform Nodes we’ll keep track of.
The next step is to make sure these two variables get set up correctly; in our PlatformNode.m file, change our init method as shown below:
if (( self = [super init] ))
platformBlockBuffer = [[NSMutableArray alloc] init];
self.platformBufferSize = 10;// Generate the first set of platform tiles on the Ground
for (int i = 0; i < self.platformBufferSize; i++)
// Generate a New Platform
All this code does is to:
- Instantiate our Platform Buffer array.
- Set our Platform Buffer size to a default value (I’ve just used 10 here because it’s easy.
- Added a loop that will fill up our Platform Buffer.
Now all we’ll need to do is to come up with a way to Generate our Platforms, which we’ll do in our next post.