




















































Read more about this book |
(For more resources on this subject, see here.)
Visit http://www.cocos2d-iphone.org for downloading the latest files. Search the downloads page, and there you will find the different versions available. As of this writing, Version 0.99.5 is the latest stable version.
Once you have downloaded the file to your computer, uncompress the folder to your desktop, and rename it to Cocos2d. Open the uncompressed folder and take a look at its contents. Inside that folder, you will find everything you will need to make a game with Cocos2d. The following is a list of the important folders and files:
We'll start by taking a look at the samples.
Inside the cocos2d-iphone folder, you will find a file named cocos2d-iphone. xcodeproj. This project contains all the samples and named tests that come with the source code. Let's run one of them.
You have just run your first Cocos2d application. If you want to run another sample, just select it by changing the "Active target". When you do so, the "active executable" should match to the same; if it doesn't select it manually by selecting it from the overview dropdown box. You can see which "active target" and "active executable" is selected from that overview dropdown box, they should be selected.
Cocos2d comes with three templates. These templates are the starting point for any Cocos2d game. They let you:
Which one you decide to use for your project depends on your needs. Right now we'll create a simple project from the first template.
Carry out the following steps for installing the templates:
cd desktop/Cocos2d
./install_template.sh
You will see a lot of output in the terminal (as shown in the following screenshot); read it to check if the templates were installed successfully.
If you are getting errors, check if you have downloaded the files correctly and uncompressed them into the desktop. If it is in another place you may get errors.
We just installed the Xcode templates for Cocos2d. Now, each time you create a new project in Xcode, you will be given the choice of doing it by using a Cocos2d application. We'll see how to do that in a moment.
Each time a new version of Cocos2d is released, there is a template file for that version. So, you should remember to install them each time. If you have a lot of older templates and you want to remove them, you can do so by going to Developer/Platforms/iPhoneOS. platform/Developer/Library/Xcode/Project Templates/Application and deleting the respective folder.
Templates are very useful and they are a great starting point for any new project. Having these templates available makes starting a new project an easy task. You will have all the Cocos2d sources arranged in groups, your AppDelegate already configured to make use of the framework and even a simple starting Layer.
Now that you have the templates ready to use, it is time to make your first project.
We are going to create a new project named HelloCocos2d from the templates you have just installed. This won't be anything like a game, but just an introduction on how to get started. The steps are as follows:
2. Cocos2d templates will appear right there along with the other Xcode project templates, as shown in the following screenshot:
Once you perform the preceding steps, the project you just created should open up. Let's take a look at the important folders that were generated:
Go ahead and run the application. Click on Build and go and congratulate yourself, as you have created your first Cocos2d project.
Let's stop for a moment and take a look at what was created here.
When you run the application you'll notice a couple of things, as follows:
In a moment, we'll see how this is achieved by taking a look at the generated classes.
We have just created our first project from one of the templates that we installed before. As you can see, using those templates makes starting a Cocos2d project quite easy and lets you get your hands on the actual game's code sooner.
The CCDirector is the class whose main purpose is scene management. It is responsible for switching scenes, setting the desired FPS, the device orientation, and a lot of other things.
The CCDirector is the class responsible for initializing OpenGL ES.
If you grab an older Cocos2d project you might notice that all Cocos2d classes have the "CC" prefix missing. Those were added recently to avoid naming problems. Objective-c doesn't have the concept of namespaces, so if Apple at some point decided to create a Director class, those would collide.
There are currently four types of directors; for most applications, you will want to use the default one:
Those are the available Directors, most of the times you will not need to make any changes here.