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

Creating a sprite using RenderTexture


RenderTexture is used to create placeholder sprites that can be used to prototype a game. So, if you want to test your movement and jump code on a sprite but don't have access to a sprite, this is a quick and dirty way to create a sprite.

Getting ready

To create the RenderTexture sprite, we will create a new function, and this function will return a sprite when we provide the size and color of the sprite to be produced.

How to do it…

In the MainScene.h file, we will add the following highlighted line right under the scene function we created earlier:

+(CCScene*)scene;

-(CCSprite *)spriteWithColor:(ccColor4F)bgColor
textureWidth:(float)textureWidth
textureHeight:(float)textureHeight;

@end 

This function will return CCSprite and take in the color, width, and height that the sprite should be of.

In the MainScene.m file, we will add the definition of the preceding function below the init function, as follows:

-(CCSprite *)spriteWithColor:(ccColor4F)bgColor
textureWidth:(float)textureWidth
textureHeight:(float)textureHeight {


  CCRenderTexture *rt = 
    [CCRenderTexture
    renderTextureWithWidth:textureWidth
    height:textureHeight];

  [rtbeginWithClear:bgColor.r
    g:bgColor.g
    b:bgColor.b
    a:bgColor.a];

    [rt end];

  return [CCSpritespriteWithTexture:rt.sprite.texture];

}

In the function, we will create a new variable called rt of the CCRenderTexture type, and to it, we will pass the width and height that is passed to the function.

Then, we will clear RenderTexture with the color that is passed in. Then, we will call the end function on rt.

Next, we will create CCSprite by passing in the texture of the rt sprite. This is then returned by the function.

How it works…

To use the RenderTexture function, we will add the following code right after where we added the background to the scene:

//rtSprite
CCSprite* rtSprite = [self spriteWithColor:ccc4f(1.0, 1.0, 0.0, 1.0) textureWidth:150textureHeight:150];
rtSprite.position = CGPointMake(winSize.width/2,  
winSize.height/2);
[selfaddChild:rtSprite];

We will create a new variable called rtSprite of the CCSprite type and assign the sprite that will be created by calling our function to it.

While calling the function, we will create a color of the ccc4f type and pass in the r, g, b, and a values. For yellow, we will pass 1 for red and green. We will provide a width and height value of 150 each.

Then, the sprite will be positioned at the center and added to the scene. Run the scene to see the result.

There's more…

The color of the sprite can be changed by changing the rgba color value.

For example, here, I changed the value to (1.0, 0.0, 1.0, 1.0) for yellow, and you can see the result as follows:

//rtSprite
CCSprite* rtSprite = [self spriteWithColor:ccc4f(1.0, 0.0, 1.0, 1.0) textureWidth:150textureHeight:150];
rtSprite.position = CGPointMake(winSize.width/2, winSize.height/2);
[selfaddChild:rtSprite];