IPhone tutorial Cocos2d Development Process

Source: Internet
Author: User

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:

 
 
  1. cd SimpleGame  
  2. git init  
  3. 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:

 
 
  1. #import <UIKit/UIKit.h>int main(int argc, char *argv[]) {  
  2. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];  
  3. int retVal = UIApplicationMain(argc, argv, nil,  
  4. @”SimpleGameAppDelegate”);  
  5. [pool release];  
  6. return retVal;  

The second step is to modify SimpleGameAppDelegate. h. The modification is as follows:

 
 
  1. #import <UIKit/UIKit.h>@interface SimpleGameAppDelegate : NSObject <UIApplicationDelegate> {  
  2. }  
  3. @end 

Finally, modify SimpleGameAppDelegate. m as follows:

 
 
  1. #import “SimpleGameAppDelegate.h”@implementation SimpleGameAppDelegate  
  2. - (void)applicationDidFinishLaunching:(UIApplication *)app {  
  3. }  
  4. @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.

 
 
  1. 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.

 
 
  1. #import <UIKit/UIKit.h> 
  2. #import “cocos2d.h”@interface MenuScene : Scene {}  
  3. @end  
  4. @interface MenuLayer : Layer {}  
  5. -(void)startGame: (id)sender;  
  6. -(void)help: (id)sender;  
  7. @end 

Next we need to implement MenuScene. m as the MenuScene and MenuLayer classes.

 
 
  1. #import “MenuScene.h”  
  2. @implementation MenuScene  
  3. - (id) init {  
  4. self = [super init];  
  5. if (self != nil) {  
  6. Sprite * bg = [Sprite spriteWithFile:@"menu.png"];  
  7. [bg setPosition:cpv(240, 160)];  
  8. [self add:bg z:0];  
  9. [self add:[MenuLayer node] z:1];  
  10. }  
  11. return self;  
  12. }  
  13. @end@implementation MenuLayer  
  14. - (id) init {  
  15. self = [super init];  
  16. if (self != nil) {  
  17. [MenuItemFont setFontSize:20];  
  18. [MenuItemFont setFontName:@"Helvetica"];  
  19. MenuItem *start = [MenuItemFont itemFromString:@"Start Game"  
  20. target:self  
  21. selector:@selector(startGame:)];  
  22. MenuItem *help = [MenuItemFont itemFromString:@"Help"  
  23. target:self  
  24. selector:@selector(help:)];  
  25. Menu *menu = [Menu menuWithItems:start, help, nil];  
  26. [menu alignItemsVertically];  
  27. [self add:menu];  
  28. }  
  29. return self;  
  30. }  
  31. -(void)startGame: (id)sender {  
  32. NSLog(@”start game”);  
  33. }  
  34. -(void)help: (id)sender {  
  35. NSLog(@”help”);  
  36. }  
  37. @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:

 
 
  1. # Import <UIKit/UIKit. h>
  2. # Import "cocos2d. h"
  3. # Import "MenuScene. h". Next, open SimpleGameAppDelegate. m and add the following code:
  4. # Import "SimpleGameAppDelegate. h"
  5. @ Implementation SimpleGameAppDelegate
  6. -(Void) applicationDidFinishLaunching :( UIApplication *) application {
  7. UIWindow * window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen] bounds];
  8. [Window setUserInteractionEnabled: YES];
  9. [Window setMultipleTouchEnabled: YES];
  10. [[Director shareddire] setLandscape: YES];
  11. [[Director shareddire] attachInWindow: window]; [window makeKeyAndVisible];
  12. MenuScene * MS = [MenuScene node];
  13. [[Director shareddire] runWithScene: ms];
  14. }
  15. @ 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:

 
 
  1. # Import <UIKit/UIKit. h>
  2. # Import "cocos2d. h" @ interface GameScene: Scene {}
  3. @ End
  4. @ Interface GameLayer: Layer {}
  5. @ End
  6.  
  7. Modify GameScene. m to make it look as follows:
  8.  
  9. # Import "GameScene. h"
  10. # Import "MenuScene. h" @ implementation GameScene
  11. -(Id) init {
  12. Self = [super init];
  13. If (self! = Nil ){
  14. Sprite * bg = [Sprite spriteWithFile: @ "game.png"];
  15. [Bg setPosition: cpv (240,160)];
  16. [Self add: bg z: 0];
  17. [Self add: [GameLayer node] z: 1];
  18. }
  19. Return self;
  20. }
  21. @ End
  22. @ Implementation GameLayer
  23. -(Id) init {
  24. Self = [super init];
  25. If (self! = Nil ){
  26. IsTouchEnabled = YES;
  27. }
  28. Return self;
  29. }
  30. -(BOOL) ccTouchesEnded :( NSSet *) touches withEvent :( UIEvent *) event {
  31. MenuScene * MS = [MenuScene node];
  32. [[Director shareddire] replaceScene: ms];
  33. Return kEventHandled;
  34. }
  35. @ End

Finally, we need to go back to the MenuLayer class to update the startGame method. The modifications are as follows:

 
 
  1. -(void)startGame: (id)sender {  
  2. GameScene * gs = [GameScene node];  
  3. [[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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.