Home > Uncategorized > Cocos2D for iPhone: Sprites

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 , ,

  1. Maarten
    April 1st, 2009 at 12:45 | #1

    How is ninja stil available in the moveStuff function? Dont you have to declare it again first?

  2. rjett
    April 4th, 2009 at 20:05 | #2

    You’re right. I was thinking that it was declared in the header file. Sorry! I’ll make a note.

  3. metronome
    April 13th, 2009 at 17:01 | #3

    I’m trying to move sprites that are in an array. Where do I declare this sprite array? I’m also populating the array within the init method, I started off with the array being created in the init function but then my timer tick function cannot access it. So where would I put in my code?;

    Thanks for your help, I’m starting out with objective c, actually the stuff you have gone over on this page is really useful :)

  4. rjett
    April 19th, 2009 at 23:06 | #4

    Thanks! I sent you an email metronome.

  1. No trackbacks yet.