Matt Tuttle


No Comments

5 Ways to Improve Your Mobile Game

Looking for ways to improve your mobile app? Here are a few ideas you can use to make your game stand out. Some of them are easier than others and you'll have to weigh the costs of development versus actual downloads.

1. New and Fresh Content

If your game is already in the app store you might want to consider adding a new theme or level to your game. This could be as simple as adding a new item or as complex as a whole new storyline. Keep it simple though as updates should probably take no longer than a week to develop.

2. Social Achievements and Leaderboards

Adding leaderboards and achievements can make your game just that much more addicting. Be creative what you use for achievements and make sure they are attainable. If you are looking for a way to use OpenFeint on the iPhone check out the tutorial on this site.

3. Add a Save Feature to Your Game

Depending on how complex your game is this could take a while to implement. However this is probably one of the more important features you should add. Most people want to be able to close your app and not lose their progress when reopening. Check out the game state tutorial if you are looking to implement this on the iPhone.

4. User-Generated Data

This takes the social aspect of your game to the next level. The ability to create objects or levels can be added to most games. Obviously this requires and editor and some way to share content over the internet which makes this improvement a bit costly. However, adding user-created content could generate a buzz about your game and provoke more purchases.

5. Port Your Game to Multiple Platforms

At the moment this is not an easy task but putting your game on another platform could greatly increase sales. We're working on a framework that makes the job of developing on multiple platforms much easier and I believe Unity 3D has taken this step as well. If you are in the early stages of development this may be something to look into.

Posted in Game Development

Tags:


No Comments

Saving Game State on the iPhone

Do you need to save your game's data on the iPhone but you don't know where to start? Look no further than this article because it should have everything you need. Just fire up Xcode and we'll begin to add our game state saving code.

First thing we want to do is create two new functions in the app delegate code. These two functions loadSettings and saveSettings will load/save our game data at the start and close of the app respectively. We'll add some boiler plate code and then move on to the fun stuff. This will save/load settings from the iPhone user defaults.

AppDelegate.mm

- (void) loadSettings {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *data = [defaults objectForKey:@"GameData"];
    gGameData = [[NSKeyedUnarchiver unarchiveObjectWithData:data] retain];
    if(gGameData == NULL) {
        gGameData = [[GameData alloc] init];
    }
}

- (void) saveSettings {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:gGameData];
    [defaults setObject:data forKey:@"GameData"];
}

- (void) applicationDidFinishLaunching:(UIApplication*)application {
    ...
    [self loadSettings];
    // Run you game code after loading settings
    ...
}

- (void)applicationWillTerminate:(UIApplication *)application {
    [self saveSettings];
    ...
}

Now let's create a GameData structure to hold all of our data we want to save and load. It's a good idea at this point to write down all of the data you want to save (score, positions, name, etc...). The implementation is pretty straight forward but can get complicated if you have a lot of data to save. In this example I'm going to save a level number.

GameData.h

#import <Foundation/Foundation.h>

@interface GameData : NSObject<NSCoding> {
    int level;
}

@property (readwrite,assign) int level;

@end

extern GameData *gGameData;

GameData.m

#import "GameData.h"
GameData *gGameData;

@implementation GameData

@synthesize level;

-(void)encodeWithCoder:(NSCoder *)coder {
    [coder encodeInt:level forKey:@"Level"];
}

-(id)initWithCoder:(NSCoder *)coder {
    NSData *data;

    if((self = [super init])) {
        [self setLevel: [coder decodeIntForKey:@"Level"]];
    }

    return self;
}

-(id) init {
    if((self = [super init])) {
        level = 1;
    }
    return self;
}

-(void) dealloc {
    [super dealloc];
}

@end

You may have noticed that we've declared a global variable gGameData in the header file. This variable will be used in your app to access the game data while running. It also makes it so we don't have to pass the variable through the entire app. The global variable is allocated and released in the app delegate so the data will always be around. You can access the GameData properties as shown.

int level = gGameData.level;
gGameData.level = level;

You can decode anything from integers, NSNumbers, booleans, and even objects. To save an object you'll need to write a custom save routine and use addObject/decodeObject respectively. 

That's about all it takes to write code to save your game's state in an iPhone app. This is obviously a simple example and you'll find that your game may require more data to save. It's also possible to save separate chunks of data in different classes using NSCoder. The most important concept is saving the data to the iPhone's user defaults. Happy coding!

Posted in Game Development

Tags:


No Comments

Integrating OpenFeint with Cocos2D for iPhone

Adding OpenFeint to your iPhone app is much easier than you might imagine. There are several steps you should take before reading this document. The first is to sign up for a developer account at http://api.openfeint.com. The second is to download the OpenFeint SDK on the developer website and unzip the files. Take note of the product key and product secret key as we will be using this later.

OpenFeint LogoThis tutorial is geared around developing apps with Cocos2D but most of this will apply to other frameworks as well. After you have downloaded the SDK you'll want to drop the OpenFeint folder into your XCode project window, I put it under frameworks. Make sure you also include additional frameworks that OpenFeint requires (libsqlite, CFNetwork, SystemConfiguration, MapKit, CoreLocation, and Security). There is a readme included with the OpenFeint SDK that would be good to read since newer versions may have different requirements. After you've got everything setup make sure it builds before we start adding any code to the project. One thing I commonly miss is adding OpenFeintPrefix.pch to my prefix header file.

Basic Setup of OpenFeint

If everything builds correctly then we can begin to integrate OpenFeint! To begin add the following lines of code to your main AppDelegate.m file. Note that you'll want to add your product and secret keys as well as the proper orientation for your app.

#include "OpenFeint.h"
- (void)initializeOpenFeint {
    NSDictionary* settings = [NSDictionary dictionaryWithObjectsAndKeys:
        [NSNumber numberWithInt:UIInterfaceOrientationPortrait], OpenFeintSettingDashboardOrientation,
        [NSNumber numberWithBool:YES], OpenFeintSettingEnablePushNotifications,
        nil
    ];
    ofDelegate = [OFDelegate new];
    OFDelegatesContainer *delegates = [OFDelegatesContainer containerWithOpenFeintDelegate:ofDelegate];
    [OpenFeint initializeWithProductKey:@"YourProductKey"
                              andSecret:@"YourSecretProductKey"
                         andDisplayName:@"YourDisplayName"
                            andSettings:settings    // see OpenFeintSettings.h
                           andDelegates:delegates];    // see OFDelegatesContainer.h
}

Also, make sure you call the function inside applicationDidFinishLaunching right before calling Cocos2D runWithScene. Now create a new class file named OFDelegate.mm and put the following code in it.

#import "cocos2d.h"
#import "TritoneOFDelegate.h"
#import "OpenFeint+UserOptions.h"
@implementation OFDelegate
- (void)dashboardWillAppear {
    [[CCDirector sharedDirector] pause];
}
- (void)dashboardDidAppear {
    [[CCDirector sharedDirector] stopAnimation];
}
- (void)dashboardWillDisappear {
}
- (void)dashboardDidDisappear {
    [[CCDirector sharedDirector] resume];
    [[CCDirector sharedDirector] startAnimation];
}
- (void)userLoggedIn:(NSString*)userId {
    OFLog(@"New user logged in! Hello %@", [OpenFeint lastLoggedInUserName]);
}
- (BOOL)showCustomOpenFeintApprovalScreen {
   return NO;
}

What this does is pause and resume the game before and after the OpenFeint window is shown. This code is specific to Cocos2D so if you are using a different framework then you can drop the pause and resume functions into the Appear and Disappear functions respectively.

Highscores and Leaderboards

Now onto the fun part! Let's create leaderboards and start sending highscores to OpenFeint. The first thing is to log into the OpenFeint website again and create a new leaderboard which is found under the Basic Features tab. Create a leaderboard and give it an appropriate name. Take note of the leaderboard ID as it will be important for your code.

Now open up your XCode project and add a new import line to the top of your main scene/layer code file.

#import "OFHighScoreService.h"

Now add the following line of code to submit a highscore to OpenFeint. It's really that easy!

[OFHighScoreService setHighScore:YourHighscore
                  forLeaderboard:@"YourLeaderBoardID"
                       onSuccess:OFDelegate()
                      onFailure:OFDelegate()];

Another thing you many want to consider is adding multiple leaderboards for different modes of gameplay. The only part you would have to change is the leaderboard ID section.

Setting up Achievements

Achievements work very similar to leaderboards except that you must assign a point value to each achievement. OpenFeint suggests creating approximately 20-30 achievements that are worth 800-1000 points in total. Of course, you are free to create as many achievements as you want within OpenFeint's limits.

Start by creating a new achievement and giving it a name. It's a good idea to add an icon that will be shown on the achievements page. OpenFeint will assign an achievement ID that you will use in your code. Drop the following code into your project to unlock achievements.

#import "OFAchievementService.h"

[OFAchievementService unlockAchievement:@"YourAchievementCode"];

Launching the OpenFeint Dashboard

Finally, it's important to add a snippet to your app to launch the OpenFeint dashboard so people can view their achievements and highscores. The easiest way to do so is adding a button that will execute the following code.

[OpenFeint launchDashboard];

Final Word of Advice

That's all it takes to integrate the most basic features of OpenFeint into your iPhone app. Make sure you test all of your achievements with test users before submitting your application to Apple. Good luck!

Posted in Game Development

Tags: