Cocos2d iPhoneThe tutorial is the content of this article,GamesAn article on 2D graphics in development. Let's take a look at the content first.Cocos2D-iPhoneIs an open-source framework, which can be easily used inIPhoneDevelop 2D games. It provides physical, sprites), time difference scroll parallax scrolling), SupportIPhoneTouch and accelerator.
There are already many applications in App Store.Cocos2DDevelopedGames-So you can also use it to make greatGame. Cocos2DDevelopers even shared some best practices for using this development framework.
It's hard to find a detailed basic tutorial and translate it into Chinese for everyone to learn.
Monocle Studios LLC recently released its first product, touchDefense.IPhone games. If noCocos2d iPhoneWe cannot make this happen so quickly.GamesThe app store is available. We should give back to this project. This White Paper is the first step.
Start Cocos2d iPhone development.
Add IDP.
The first step is to register as a professional iPhone developer. it is free for Development (although $99 is required when you need to run your program on a real machine), it is much faster than other methods.
Next, you need to download the iPhone SDK. The download project may be a little large. You may wish to download it as quickly as possible.
XCode entry.
Next, we need to create a standard iPhone project in XCode.
Click File> New Project.
Select Application in the iPhone OS Column
Finally, select the Window-Based Application template.
Click Choos
Next, name this project (We will name it SimpleGame. In this example, you will name it like us, and it will not be confusing when you read the tutorial ), and save it to a proper location. next, the project will be opened, for example:
Add Version Control for your project now! We like to use Git, which makes creating a new project library very fast:
- cd SimpleGame
- git init
- git commit -a -m "Initial commit."
You may prefer Mercurial or SVN. They are also great. Use them as you like!
Before proceeding, click the Build and Go button.IPhoneThe simulator saw a white screen (this was the first appearance of SimpleGame ).
Remove the window and nib files.
Based onCocos2d iPhoneThe project does not need to use nib or standard UI components, so we need to make some modifications to the default project created by XCode. (unfortunately, when usingCocos2d iPhoneThere is no perfect template that can be used without modification). First, delete the MainWindow. xib file.
Then, delete the content of Main nib file base name in the Info. plist file.
Next, we will modify some source files to replace some content in main. m. The replacement result is as follows:
- #import <UIKit/UIKit.h>int main(int argc, char *argv[]) {
- NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
- int retVal = UIApplicationMain(argc, argv, nil,
- @”SimpleGameAppDelegate”);
- [pool release];
- return retVal;
- }
The second step is to modify SimpleGameAppDelegate. h. The modification is as follows:
- #import <UIKit/UIKit.h>@interface SimpleGameAppDelegate : NSObject <UIApplicationDelegate> {
- }
- @end
Finally, modify SimpleGameAppDelegate. m as follows:
- #import “SimpleGameAppDelegate.h”@implementation SimpleGameAppDelegate
- - (void)applicationDidFinishLaunching:(UIApplication *)app {
- }
- @end
After completing these changes, you can ignore them. When you start a new project, remember to go back and check it out. (It is best to create a template for the Cocos2d iPhone project, then you can do it once and for all ).
Integrate Cocos2d iPhone.
The next step is to capture the latest Cocos2d iPhone library from Google Code. Go to the Cocos2d iPhone download page and select version 0.7.0 to download it.
- tar -xvf cocos2d-iphone-0.7.0.tar
Next, go back to your project.
Select Project menu> Add Files to Project... Then, browse the directory you just decompressed and add this directory to your project. You can select the check box before Copy items into destination group's folder (if needed, click Add.
Next, this is an important step to delete the Demo folder under the External \ Chipmunk directory. after that, you can first compile your project to ensure there is no problem. (Problems in the compilation process are early to be fixed, and it will be a lot of trouble to find problems later)
Now, go back to the Project window and select Project-> Add Files to Project to Add the cocos2d-iphone-0.7.0/cocos2d folder to your Project.
Repeat the above operation to add the cocos2d-iphone-0.7.0/cocoslive and cocos2d-iphone-0.7.0/experimental folders to your project.
Finally, add the cocos2d-iphone-0.7.0/cocos2d/Resources/fps_images.png to the Resources Group in your project.
We have configured the cocos2d-iphone, but this project has not been compiled, find targets in the left column, click the small arrow on the left side of SimpleGame to expand it, select Link Binary With Libraries, right-click and choose Add> Add Existing Frameworks, just as in.
In the pop-up window, click the "+" button under the window (below the Linked Libraries list), and select OpenGLES. framework and QuartzCore. framework.
After you click the add button, your linked libraries list should be the same.
Now, when you build your project, they will be compiled into the project. Of course, there is no way to do anything, but the compilation fails, which is the best.
Sort projects.
Let's organize the project structure efficiently. Now it's hard to find out the SimpleGameAppDelegate file, because a lot of things are displayed at the root level of the project.
Create two Groups under the project: Support and Classes
First move Chipmunk and cocos2d to the Support group. Then move SimpleGameAppDelegate. h and SimpleGameAppDelegate. m to the Classes group. After completing the operation, your project level will look like:
This is not necessary, but keeping the project level clear will make your work get twice the result with half the effort.
Create a main menu.
Now we have to start usingCocos2d iPhoneBefore writing the code, let's take a few minutes to give an overview. We will use it in the next course.
InCocos2d iPhone, You can easily and quickly process the scene Scence, Layer, etc. scene Scence is a scenario that players can see after you create it. it is composed of one or more layers. to display a specific scenario Scence, you need to tell Director (this is a singleton mode class) to display it.
Now we want to create MenuScene, which is a subclass of a scenario. We also need to create a MenuLayer which will be included in our menu.
Finally, the applicationDidFinishLaunching party we want to connect notifies Dorector to execute MenuScene.
We need to add an image to the project as the background of the MENU layer. You can download this image and add it to your project.
Now I am writing code. right-click the Classes group and choose Add-> New File ..., select the NSObject subclass file type in Cocoa Touch Classes and name it MenuScene. m. h header file)
Now open the MenuScene. h file and change the code as follows.
- #import <UIKit/UIKit.h>
- #import “cocos2d.h”@interface MenuScene : Scene {}
- @end
- @interface MenuLayer : Layer {}
- -(void)startGame: (id)sender;
- -(void)help: (id)sender;
- @end
Next we need to implement MenuScene. m as the MenuScene and MenuLayer classes.
- #import “MenuScene.h”
- @implementation MenuScene
- - (id) init {
- self = [super init];
- if (self != nil) {
- Sprite * bg = [Sprite spriteWithFile:@"menu.png"];
- [bg setPosition:cpv(240, 160)];
- [self add:bg z:0];
- [self add:[MenuLayer node] z:1];
- }
- return self;
- }
- @end@implementation MenuLayer
- - (id) init {
- self = [super init];
- if (self != nil) {
- [MenuItemFont setFontSize:20];
- [MenuItemFont setFontName:@"Helvetica"];
- MenuItem *start = [MenuItemFont itemFromString:@"Start Game"
- target:self
- selector:@selector(startGame:)];
- MenuItem *help = [MenuItemFont itemFromString:@"Help"
- target:self
- selector:@selector(help:)];
- Menu *menu = [Menu menuWithItems:start, help, nil];
- [menu alignItemsVertically];
- [self add:menu];
- }
- return self;
- }
- -(void)startGame: (id)sender {
- NSLog(@”start game”);
- }
- -(void)help: (id)sender {
- NSLog(@”help”);
- }
- @end
Now let's put down the startGame and help methods, and we will update them later.
Now that we have an executable menu, we need to open the SimpleGameAppDelegate (. h and. m) file so that Director loads MenuScene when the application starts.
Add cocos2d and MenuScene import to the top of SimpleGameAppDelegate. h:
- # Import <UIKit/UIKit. h>
- # Import "cocos2d. h"
- # Import "MenuScene. h". Next, open SimpleGameAppDelegate. m and add the following code:
- # Import "SimpleGameAppDelegate. h"
- @ Implementation SimpleGameAppDelegate
- -(Void) applicationDidFinishLaunching :( UIApplication *) application {
- UIWindow * window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen] bounds];
- [Window setUserInteractionEnabled: YES];
- [Window setMultipleTouchEnabled: YES];
- [[Director shareddire] setLandscape: YES];
- [[Director shareddire] attachInWindow: window]; [window makeKeyAndVisible];
- MenuScene * MS = [MenuScene node];
- [[Director shareddire] runWithScene: ms];
- }
- @ End
Now you can Build and Go, and you will see the ugly figure below.
Fortunately, we have learned a lot about Cocos2D usage, which will make the subsequent things much easier.
Add a game scenario.
Next, we will create a simple Game scenario, which will be displayed after you click the Start Game option. let's add another different background image, which will be realized when we enter the game.
Add this image to the project and it will becomeGamesBackground of the scenario.
Create the GameScene. h and GameScene. m files to the Classes group. Add the following code to the GameScene. h file:
- # Import <UIKit/UIKit. h>
- # Import "cocos2d. h" @ interface GameScene: Scene {}
- @ End
- @ Interface GameLayer: Layer {}
- @ End
-
- Modify GameScene. m to make it look as follows:
-
- # Import "GameScene. h"
- # Import "MenuScene. h" @ implementation GameScene
- -(Id) init {
- Self = [super init];
- If (self! = Nil ){
- Sprite * bg = [Sprite spriteWithFile: @ "game.png"];
- [Bg setPosition: cpv (240,160)];
- [Self add: bg z: 0];
- [Self add: [GameLayer node] z: 1];
- }
- Return self;
- }
- @ End
- @ Implementation GameLayer
- -(Id) init {
- Self = [super init];
- If (self! = Nil ){
- IsTouchEnabled = YES;
- }
- Return self;
- }
- -(BOOL) ccTouchesEnded :( NSSet *) touches withEvent :( UIEvent *) event {
- MenuScene * MS = [MenuScene node];
- [[Director shareddire] replaceScene: ms];
- Return kEventHandled;
- }
- @ End
Finally, we need to go back to the MenuLayer class to update the startGame method. The modifications are as follows:
- -(void)startGame: (id)sender {
- GameScene * gs = [GameScene node];
- [[Director sharedDirector] replaceScene:gs];
- }
Be bold in creating this project and immerse yourself in the joy of success for several seconds.Cocos2d iPhoneThe best thing is that everything becomes simple from here, and the difficult part is over.
The teaching is over now.
You can download the project file we created for this tutorial.
We hope this will help you with the introduction.Cocos2d iPhoneThe role of this project allows you to take a moment to understand this great project.
Summary:Cocos2d iPhoneI hope this article will be helpful to you! MoreGamesFor development content, see edit recommendations!
Original post address http://www.cocoachina.com/bbs/read.php? Tid-2697-fpage-11.html