The Object Oriented Game Approach Part 2: Getting started with iPhone Game development
In the past articles, I’ve discussed OOP(Object Oriented Programming) and Game Programming.Be sure to at have a grasp of the basics before diving to this next article.
The basic prerequisites to develop your first iPhone GameApp are:
1)Objective C
2)the iPhone SDK
3)MAC:XCode
So to develop a game in the iPhone , these prerequisites will force you to work in a MAC, and learn Objective C on top of learning the iPhone SDK. Do not be alarmed. Take one step at a time.If you know OOP already, then you just have to familiarize.
First let us tackle Objective C.This next article is an Object C Primer which will prepare you for the iPhone Game Development.
Objective C is a combination of OOP + C. As such, it can be compared to C++ in being object oriented with the feel of our good old ANSI C.You may or may not have been trained in C so let us look at the differences first.
Syntax:
This is a hello world program in C. This also works in object C. There isn’t much difference when it comes to syntax.
#import <stdio.h>
int main( int argc, const char *argv[] ) {
printf( "hello world\n" );
return 0;
}
Paradigm:
OOP vs Procedural. In ANSI C, you learned to program using procedures,but in Object C, we work with objects.
4 things that you should be familiar :
a)Objects
b)Class
c)Methods
d)Memory Management
a)Objects-Objects are what makes OOP.
-creating objects
i)manual-you initialize this object, so you have to remove it later
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init]
ii)automatic-this has some relation with the automatic memory management
Integer *num1 = [Integer new]
iii)Other examples:
//An array object; nil is equivalent to NULL
NSMutableArray *aRR= nil
//A string
NSString* myString = [NSString string];
//A number with initialized value
NSNumber* num= [[NSNumber alloc] initWithFloat:10.0];
b)Class
You need a yourClass.h and yourClass.m files
+yourClass.h contains:
-instance variables
-public methods.
+yourClass.m contain the methods
eg. this file is named “main.m”
#import <UIKit/UIKit.h>
int main(int argc,char *argv[]){
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init]
int retVal=UIApplicationMain(argc,argv,nil,nil);
[pool release];
//since the object pool has been initialized manually, you need to manually remove it
return retVal
}
c)Methods
Remember about setters and getters method?Setters set values, while getters get values.
- (NSString*) string; //.a getter
- (void) setString: (NSString*)input; // a setter
Some notes;
A dash before means that it’s an instanceinstance method.
A plus beforemeans it’s a class method.
d)Memory Management
By now, you should have learned that memory is valuable. You can have lots of it, but without learning how to do some cleanup, you’ll probably leave a lot of clutter. We’ve learned that objects can be manually or automaticall instantiated. Doing it manually gives you better control of the object, while doing it automatically means that you won’t have to worry about not deallocating the objects.
eg.
(void) dealloc{
[string release]; //deallocates the string instance variable
[super dealloc]; //calls the superclass to clean up itself to prevent memory leak
}
Remember this simple rule:”What you allocate, deallocate.”
You cannot learn everything about OOP in this article, but this will be enough to get you started in your development.
Now are you ready for the iPhone?

