The goal of this article is to learn how to work with sprites and get to know their main properties. After reading this article, you will be able to add sprites to your games.
In this article, we will cover the following topics:
We could create many separate mini projects, each demonstrating a single Cocos2D aspect, but this way we won't learn how to make a complete game. Instead, we're going to create a game that will demonstrate every aspect of Cocos2D that we learn.
The game we're going to make will be about hunting. Not that I'm a fan of hunting, but taking into account the material we need to cover and practically use in the game's code, a hunting game looks like the perfect candidate.
The following is a screenshot from the game we're going to develop. It will have several levels demonstrating several different aspects of Cocos2D in action:
Let's start creating this game by creating a new Xcode project using the Cocos2D template, just as we did with HelloWorld project, using the following steps:
Make sure that you select the correct simulator version to use. This project will support iPhone, iPhone Retina (3.5-inch), iPhone Retina (4-inch), and iPhone Retina (4-inch, 64-bit) simulators, or an actual iPhone 3GS or newer device running iOS 5.0 or higher.
Now, we have a project that we'll be working on.
The project creation part should be very similar to the process of creating the HelloWorld project, so let's keep the tempo and move on.
As we're going to work on this project for some time, let's keep everything clean and tidy by performing the following steps:
Right-clicking on the group and selecting New File instead of using File | New | File will place our new file in the selected group after creation.
You can create the Scenes folder while in the save dialog using New Folder button and save the GameScene class there. This way, the hierarchy of groups in Xcode will match the physical folders hierarchy on the disk. This is the way I'm going to do this so that you can easily find any file in the book's supporting file's projects.
However, the groups and files organization within groups will be identical, so you can always just open the Cocohunt.xcodeproj project and review the code in Xcode.
#import "IntroScene.h" #import "HelloWorldScene.h"
It is important to remove these #import directives or we will get errors as we removed the files they are referencing.
#import "GameScene.h"
-(CCScene *)startScene { return [[GameScene alloc] init]; }
We've just created another project using the Cocos2D template. Most of the steps should be familiar as we have already done them in the past.
After creating the project, we removed the unneeded files generated by the Cocos2D template, just as you will do most of the time when creating a new project, since most of the time you don't need those example scenes in your project.
We're going to work on this project for some time and it is best to start organizing things well right away. This is why we've created a new group to contain our game scene files. We'll add more groups to the project later.
As a final step, we've created our GameScene scene and displayed it on the screen at the start of the game. This is very similar to what we did in our HelloWorld project, so you shouldn't have any difficulties with it.