Book Image

Cocos2d Cross-Platform Game Development Cookbook - Second Edition - Second Edition

Book Image

Cocos2d Cross-Platform Game Development Cookbook - Second Edition - Second Edition

Overview of this book

Cocos2d is the world’s leading game development framework for developing iOS games. With the introduction of Swift and Spritebuilder, it has become easier than ever to develop the games of your dreams without much effort. With Cocos2d, you can also deploy the game on Android, thereby maximizing profit and reducing development and porting costs. The book starts off with a detailed look at how to implement sprites and animations into your game to make it livelier. You will then learn to add scenes to the game such as the gameplay scene and options scene and create menus and buttons in these scenes, as well as creating transitions between them. From there on, you will get an understanding of how to program user interactions such as tapping, holding, and swiping. You’ll then add accelerometer inputs and physics to the scene, and make objects respond back to the inputs. A game is practically incomplete without audio being added, so this will be covered next. The next section will include ways to add Artificial Intelligence to enemies in the game, allowing them to patrol, chase, and shoot in a projectile manner. You will then learn to use NSUserDefault to save and load game progress, and create and access files using JSON, Plist, and XML files for custom storage and retrieval of data. Then you will learn to add dynamic lighting to your game and will use industry-wide tools such as Texture Packer, Glyph Designer, Physics Editor, Particle Designer, and Sprite Illuminator to create more visually appealing and performance-optimized games. Towards the end of the book, we dive into Apple’s latest programming language—Swift, highlighting the major differences between Objective C and Swift. The book culminates with taking your existing game developed for iOS and porting it to Android, showing you how to install the Android Xcode plugin as well.
Table of Contents (19 chapters)
Cocos2d Cross-Platform Game Development Cookbook Second Edition
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Getting access to MainScene


When you launch the application, the scene created in SpriteBuilder will load up by default. We will have to make some minor changes to get access to the MainScene file and load this as default instead.

Getting ready

As of now, both the MainScene.h and MainScene.m files have nothing in them. So, open them up and add the following code in them.

How to do it…

First, open up MainScene.h and add the following highlighted code:

@interface MainScene :CCNode{

CGSizewinSize;    
}

+(CCScene*)scene;

@end

Next, in the MainScene.m file, add the following:

#import "MainScene.h

@implementation MainScene

+(CCScene*)scene{

  return[[self alloc]init];

}

-(id)init{

  if(self = [super init]){

  winSize = [[CCDirectorsharedDirector]viewSize];

  }

  return self;
}

@end

Then, navigate to the AppDelegate.m file, which is in the iOS group under Source/Platforms, as shown in the following screenshot:

Change the code in the startScene function as highlighted here:

- (CCScene*) startScene
{

  //Comment or delete line below as shown
  //return [CCBReaderloadAsScene:@"MainScene"];

  //add below line instead
  return [MainScene scene];
}

Now, we have a complete blank project to work with.

If you build and run the project now, you will see nothing but a black screen. To make sure that we are actually ready to draw something and that it will get displayed onscreen, let's add some basic code to change the background color.

Add the following code to the init function of the MainScene.m file:

-(id)init{

  if(self = [super init]){

    winSize = [[CCDirectorsharedDirector]viewSize];

    CGPoint center = CGPointMake(winSize.width/2,
      winSize.height/2);

    //Background
    CCNode* backgroundColorNode = [CCNodeColor
      nodeWithColor:[CCColor
      colorWithRed:0.0f
      green:1.0
      blue:0.0]];
    [selfaddChild:backgroundColorNode];


  }

  return self;
}

In the init function, after initializing the super init file, we will first get the screen size of the current device. Then, we will have a helper CGPoint variable, which is used to calculate the center of the screen.

Then, we will create a new CCNode and call it backgroundColorNode and call the CCNodeColor class and the nodeWithColor function. In it, we will pass the red, green, and blue values. As I wanted a green background, I have the value of green as 1 and the rest are 0.

Then, we will add backgroundColorNode to the scene.

How it works…

Run the project to see the changes.

You will just see a green screen, which is the node that was just added to the scene. This can be used if you want to have a plain background, and instead of importing an image into the project, this is a quick way of having any colored background.