Category Archives: Code

This is a compilation of tips, tricks, and best practices that I’ve accrued as I progress through various project development.

Designing a Custom UITableViewCell with a XIB File

Here’s a quick little Got’cha that crops up when you’re making a custom UICollectionView Cell.

I like to design my custom cells with their own XIB files, so that I can edit them and set up their connection independent of their corresponding CollectionView of TableView class files. This level of abstraction just helps me to conceptualize my code.

But when doing this, it’s important to make the IBOutlet connections to the Cell object and not the File’s Owner, rather than the normal case of making those connections to the File Owner’s class.

So it’s important to keep in ming which connections need to made and to what.

presentScene Error

So I’ve made this mistake twice recently, and anytime that happens I tend to thoroughly document my problem and it’s solution. It’ s a kind of “Fool me Once” Attitude.

So this neat little error occurs when I’ve made a new UIView and try to add a new SKScene to the ViewController. When you add an SKScene, you’ll use the SKView  presentScene method to do so. Normally, you’ll fetch the main view of your presenting UIViewContoller to do this, but when I’ve added a brand spanking new UIViewController to my project and then attempt to present an SKScene, I’ll get this error:

Shot-1

This error message pops up because we’re trying to send the presentScene selector to a UIView, instead of an SKView.

You see, when you make a new UIViewController it’s view object will default to an instance of UIView instead of an SKView.

Fixing this is pretty simple, though.

You just have to change the class of your new UIViewController’s view object in the interface builder to an SKView.

Shot-2

 

And now you’re ready to rock and roll!

Red Sun Prototype

It’s important to test early and test often in order to get the best product possible.

Prince Albert had one of the world’s largest diamonds, the Koh-i-Noor, repeatedly re-cut in order to get the perfect shine to it, and that’s what we’ll be doing with Project Red Sun. Unfortunately for Prince Albert, this constant re-cutting decreased the size of the diamond by 40%! So hopefully, we’ll have a better idea of when to stop polishing.

To do that, we’re working on a quick prototype app to test the trickiest bits of functionality (with a loveable Pirate Theme!).

We’ll keep you apprised of our prototype project and we edge our way closer to a successful launch!

 Fingers (and swords) crossed!

Using Two CollectionViews

Something I’ve noticed myself doing is that I’ve been needing to set up more than one Collection View in a View Controller. This isn’t something that happens a lot, but when it does, it’s good to have an example or two in your notes from which to work.

The only real new functionality is to add some If-Then Statements to some of our CollectionView Data Source Methods in order to check on which Collection View is requesting data.

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {

    if ( view == collectionView1 ) {

// Return the Number of Cells in collectionView1

   }

    if ( view ==collectionView2) {

// Return the Number of Cells in collectionView2;

   }

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)view cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    if ( view == collectionView1 ) {

// Return the Cells in collectionView1

   }

    if ( view ==collectionView2) {

// Return the Cells in collectionView2;

   }

}

All in all, it’s pretty simple.

Apportable

It’s taken me quite a bit of work to get as proficient with iOS and Objective C as I am. I’m not going to downplay the amount of time and energy I’ve spent on my skills, while simultaneously working full-time and going to graduate school full-time. It’s hard.

But iOS captures only about 45% of the Mobile Platform market. Over half of Mobile Devices are running Android now, and every developer needs to be at least familiar with the language.

But that means another 8 months of sleepless nights while crawling through the introductory exercises of a new library. Doesn’t it? Nope.

There are plenty of tools to compile Objective C code to the Android Platform. I started working with the Apportable Engine.

I’m a little skeptical, but I’m optimistic that this will really cut-down on the Android learning curve, especially for Video Game Development.

I’ll be posting my progress and some of the more, memorable, bugs I run into while using the Apportable Engine in the hopes that it might make some people’s lives a bit easier.

Compiling to Android

I ran into a really interesting, some might say frustrating, quirk of the Apportable Engine.

In order to get XCode to compile on the Android OS, it turns out that you have to explicitly add any file or resource that your project is going to use to your project.

In order to do this, first navigate to the Build Phase Tab in you Project Navigator.

Then, in the Compile Sources Section you’ll have to manually add references to your Class files.

Apportable_Adding Code

Then, in the Copy Bundle Resources Section you’ll have to manually add references to you resource file.

Apportable_Adding Reseources

 

And that should clear up the error.

Positioning the Platforms – Y Now!?

Last time we nailed down the X-Coordinate of our Platform Nodes, and this time we’ll dig into the much more difficult task of picking a Y-Coordinate. In our last post, we just left the Coordinate as a value of 4, which will make a single unbroken platform. (Pretty boring right?)

But now it’s time to add a little challenge!

The primary aspect of platform games is the ability of the player to jump from one platform to another, essentially making an obstacles course. Sometimes there can be monsters, or treasures, or traps, but at their heart, platform games really come down to that simple premise.

So to procedurally generate this feeling, our first step is to start programmatically carrying the height of our platforms as they scroll across our screen.

To do this, we’re going to take some time to do a bit of design effort that will save us some time down the road.

What we’re going to do is add a new class to our project that encapsulates our height calculation algorithm. This design principle is called the Strategy Pattern, and is the best pattern to encapsulate complicated algorithm that might change over time. For now, we’ll be keeping our algorithm simple, but we may want to change it over time, and this design pattern will help make that easier.

So we’ll make a new class in our project and call it  EEPlatformGenerator.

Now that we have our strategy class, we can get to work.

But, there’s no sense getting ourselves wound around the axel immediately, so we’ll implement the simplest possible algorithm that we can. The simplest way to vary the height of our platforms is to just make it random. To do this, we’ll spin a quick helper function to generate a random number between two values, a maximum and minimum value:

Method - Random Number

Now all we have to decide what those maximum and minimum values are going to be. So we’re going add some data to our new class:

Class - EP Platform Method

What this data does is:

  1. currentBlockHeight – This datum will track the Block Height of the last platform.
  2. jumpHeight – This datum is the height our player can achieve when he jumps.
  3. dropHeight – This datum is the height form which a player can fall without taking damage.
  4. platformCielingHeight – This datum will provide the maximum height of a platform. We’ll use this to prevent our platforms from climbing to ridiculous heights, we wouldn’t want a platform with a height of 145,057.

Now we’ll use our new data to build a method to determine the height of our next platform:

Method - heightOfPlatform_01

 

Ta Da!!!

Positioning the Platforms – Dos Equis

Last time we built a function to add Platforms to our Scene at a specified point, so we can place our platforms wherever we want! All we need is an X-Y Coordinate in our tile grid that we’ve already developed here:

But just getting the platforms scrolling along our scene is really the easy part; the tricky part is developing a procedure to place them in order to make our game looked like a pre-designed platform level.

There are two parts to this issue: getting an X-coordinate, and getting a Y-coordinate. The first part will be fairly easy, and the second part will take a bit more design work.

To select our Platform’s X-Coordinate, we’ll use the platform buffer we developed previously.

If you recall, the entire point of our platform buffer was to hold all of our Platform Nodes as they scroll across the screen. This means that if  we can visualize our platform buffer as a line of Platform Nodes positioned across the screen, with the first Platform Node in our Platform Buffer as being the first Platform Node in a line of across the screen and the last Platform Node in our Platform Buffer as being the last Platform Node in a line of across the screen.

This is a pretty key concept, here! What this concept allows us to do is simply use a Platform’s position in the Platform Buffer as it’s X-Coordinate.

We’ll implement this in a new method below:

AddPlatform_01

This method will add a new platform, using the generate platform method we developed previously. We place the platform at the end position of our platform buffer.

For now we’ve got the platform’s X-coordinate sorted out, and we just place at a height of 4 four now, but we’ll be changing that later.

Coordinate Conversions

Last time, I held off on going over our method to add Platforms to our SKScene. The reason I did this is because I wanted to take some time to go over a bit of background before diving headfirst into the code.

This time I’m going to go over the coordinate system that we’ll use to place our Platforms.

The strategy that we’ll use is to break our SKScene into a grid. Every column of our grid will have exactly one Platform, so the width of our grid will be the size of our  Platform Buffer and the size of each square of our grid will be the size of our Platforms.

The trick is going to be finding a way to change the coordinates of our grid to the coordinates of our SKScene.

To do this, we’ll add two new methods to our PlatformNode  class:

- (CGPoint) convertGridCoordinate ToSceneCoorindate:(CGPoint)gridCoordinate {return CGPointMake(gridCoordinate .x * self.platformBrickTexture.size.width,
gridCoordinate .y * self.platformBrickTexture.size.height + self.platformBrickTexture.size.height / 2);}
– (CGPoint) convertSceneCoorindateToGridCoordinate :(CGPoint)sceneCoorindate
{return CGPointMake(roundf(sceneCoorindate.x / self.platformBrickTexture.size.width),
roundf((sceneCoorindate.y – self.platformBrickTexture.size.height/2)/self.platformBrickTexture.size.height) );}

 

These two methods will convert a CGPoint between the coordinate system of our grid and the coordinate system of our SKScene.