Archive

Archive for March, 2009

Removing the status bar from an iPhone application

March 28th, 2009

To remove the status bar from your application simply add the following line of code to your project:

 
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
 

You can also edit your plist file and add a key for UIStatusBarHidden and set its value to true.

rjett Uncategorized , , ,

Cocos2D for iPhone: Sprites

March 26th, 2009

Setting Cocos2D to portrait mode

Before we get started let's switch to portrait mode.

In XCode, locate your delegate class implementation. If you followed the Monocle Studios example given in the previous post the file you are looking for is named "SimpleGameAppDelegate.m".

Under the applicationDidFinishLaunching function you should find a line of code that looks something like this:

[[Director sharedDirector] setLandscape: YES];

To force your application to be in portrait mode you simple change the YES to a NO:

[[Director sharedDirector] setLandscape: NO];

You could also just delete or comment out the line. That also seems to serve the same function.

Loading Sprites

Cocos2D makes it ridiculously easy to load a sprite. First you want to right click on the Resources folder in your navigation pane in XCode. Then you want to click Add -> Existing files. Locate the .png file you want to load in as your sprite and click ok. In the next window you want to check "Copy items into destination group's folder (if needed)." Then, click "Add".

In a game layer you can easily load the sprite you just added to your project with one line of code:

 Sprite * ninja = [Sprite spriteWithFile:@"ninjaSprite.png"];

If you want to position the sprite you can do so easily by using the following line:

[ninja setPosition:cpv(50, 50)];

To have Cocos display this sprite you have to add it. To do this you would use the following line:

[self addChild: ninja];

In case you missed it elsewhere, cpv creates a Chipmunk vector. Chipmunk vectors are used by Cocos to specify x and y coordinates of objects (e.g. cpv(xCoordinateHere, yCoordinateHere).

Moving a Sprite by using a timer

Once your sprite has been loaded you can easily move it by using a timer. Cocoa has its own timers, but the authors of Cocos2d for iPhone consider using their scheduler to be the best practice for timer based behaviors. To use the Cocos2d scheduler you simply need to specify a function to call. For example:

 
[self schedule: @selector(moveStuff:)];
 

In the above example, moveStuff is the name of the method that the scheduler will call. You can also add an interval when you schedule a function to be called. For example:

 
[self schedule: @selector(moveStuff:) interval: 0.5]; // half second interval
 

In my experience, scheduling a function to be called without specifying an interval tells Cocos2D to call this function as often as possible without affecting performance. The function that the scheduler will call needs to conform to the interface that Cocos2D expects. Here is an example of a moveStuff function:

 
-(void) moveStuff: (ccTime) dt
{
        t += dt; //t is a global float that I initially set to 0.0f
        ninja.position = cpv(10*sin(dt), 380); //moves the sprite back
        //(let's assume we have pointer to ninja in the header file for this class).
        //and forth on the x-axis
}
 

rjett Uncategorized , ,

Cocos2D: An Introduction

March 22nd, 2009

What?

Cocos2D is a framework for creating 2D games in Python. Cocos2D has been ported from Python to Objective-C for use on the iPhone. Cocos2D has a lot of great functionality that a developer can adopt for a mobile game including sprite management, basic menus, 2D physics (via Chipmunk), particle system, parallax scrolling, and more.

Understanding Cocos2D

Cocos2D is based around the concepts of scenes. A scene could be a menu, game level, options, menu, credits screen, etc. Cocos also has something called layers. Layers in Cocos2D are means of organizing your code within your scene.

Getting started

To get started with Cocos2D for iPhone I would highly recommend this tutorial from Monocle Studios. It will get you set up with a nice clean ready to go Cocos2D template. All the examples that I will be giving later in this blog assume that you already have a working Cocos2D template.

rjett Uncategorized , ,

iPhone OpenGL tips

March 22nd, 2009

I'd strongly recommend using Jeff LaMarche's iPhone OpenGL ES template

I'd also recommend you getting your hands on Texture2D.h. This is a file that comes from Apple that you can find attached to various demos or code example. It makes it really easy to load and attach a texture in OpenGL ES.

For 2D game development on the iPhone check out Cocos2d. I've been tinkering with it lately and it is quite good. You don't even have to bother with OpenGL ES if you use Cocos2d and it comes with Chipmunk, an awesome 2D physics engine.

I'm going to post more about how to use Cocos2D for iPhone game development later. I've been busy learning to use it myself.

rjett Uncategorized

Using the iPhone accelerometer

March 14th, 2009

I've discovered that it is quite easy to gain access to the iPhone accelerometer. Below are some simple code fragments that can be utilized to gain access to the sensor:

In the header of an Objective-C interface you need to add the protocol for the accelerometer. In my example I am going to add it to an EAGLView, which is a subclass of UIView used for OpenGL ES:

 
@interface EAGLView : UIView <UIAccelerometerDelegate> {
   //your code here
}
//your methods
- (void)accelerometer:(UIAccelerometer *)accelerometer
      didAccelerate:(UIAcceleration *)acceleration;
@end
 

In your implementation file you simply need to add the following lines in an init function, or in the case of EAGLView you could add them in initWithCoder:

 
//You may need to change the interval to optimize your app's performance:
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(0.1f)];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
 

The final line you need to add to the implementation file is the function we added to the header:

 
- (void) accelerometer:(UIAccelerometer*)
accelerometer didAccelerate:(UIAcceleration*)acceleration {
	NSLog(@"X: %g, Y: %g, Z: %g", acceleration.x,
             acceleration.y, acceleration.z);
}
 

rjett Uncategorized

Cocoa Foundation: NSArray and NSMutableArray

March 2nd, 2009

NSArray and NSMutableArray are two classes provided by Apple in the Cocoa Foundation Framework. The main different between the two is mutability. Once created, an instance of NSArray cannot be changed (is not mutable), while NSMutableArray can be changed.

NSArray will hold only Objective-C objects. You can't put C types, like int, float, enum, struct, or pointers in an NSArray. You also can't store nil in an NSArray, as it is used to indicate the end of an array during allocation.

NSArray Example Code

 
#import <Foundation/Foundation.h>
 
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
	//NSArray Magic:
 
	//Creating an Array
	NSArray *array = [NSArray arrayWithObjects: @"One", @"Two", nil];
 
	//Accessing an Array's contents using NSEnumerator
	NSEnumerator *enumerator = [array objectEnumerator];
    id obj;
 
    while ( obj = [enumerator nextObject] ) {
       NSLog( @"%@", obj);
    }
 
	//You could use NSLog to dump the array contents like so
	//NSLog(@"%@", array);
 
    [pool release];
    return 0;
}
 

NSMutableArray Example Code

 
#import <Foundation/Foundation.h>
 
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
	//NSMutableArray Magic:
 
	//Creating a mutable array!
	//A mutable array will expand as needed, but we
        //must specify an inital capcity:
	NSMutableArray *array = [NSMutableArray arrayWithCapacity: 2];
 
	//Add an object
	[array addObject: @"One"];
 
	//Add another object
	[array addObject: @"Three"];
 
	//Insert an object at a particular index
	[array insertObject: @"Two" atIndex: 1];
 
	//Accessing an Array's contents using NSEnumerator
	NSEnumerator *enumerator = [array objectEnumerator];
    id obj;
 
    while ( obj = [enumerator nextObject] ) {
       NSLog( @"%@", obj);
    }
 
    [pool release];
    return 0;
}
 

rjett Uncategorized

Cocoa Foundation

March 2nd, 2009

What is Cocoa Foundation? Cocoa Foundation is a framework of all the basic building blocks a developer will need. According to Apple, the goal of the framework is to, "Provide a small set of basic utility classes, Make software development easier by introducing consistent conventions for things such as deallocation, Support Unicode strings, object persistence, and object distribution, and Provide a level of OS independence to enhance portability."

Cocoa Foundation provides a lot of basic functionality that helps developers manage XML, Strings, Collections, Net Services, Threading, and more.

rjett Uncategorized