Book Image

Mastering Cocos2d Game Development

By : Alex Ogorek
Book Image

Mastering Cocos2d Game Development

By: Alex Ogorek

Overview of this book

Table of Contents (15 chapters)
Mastering Cocos2d Game Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating buttons via CCButton and CCLayout


Cocos2d 3.0 changed the way buttons are displayed. If you've used previous versions of Cocos2d, you're probably familiar with CCMenu. That is no longer the way to create and display tappable buttons in Cocos2d. Instead, we're going to use CCButton and place them in a node of the CCLayout type. If you skipped the sprite sheet section, I strongly recommend that you go back and read it. It will save you from many frustrating moments as the project progresses.

For the book's project, we'll be adding the menu button in the bottom-left corner. Like I said, it's extremely easy to add the buttons once you have the images included in the project.

Open the MainScene.m file, and add these lines of code below the code for the labels in the init method:

CCButton *btnMenu = [CCButton buttonWithTitle:@""
  spriteFrame:[CCSpriteFrame frameWithImageNamed:@"btnMenu.png"]];
btnMenu.position = ccp(winSize.width * 0.125, winSize.height * 0.1);
[self addChild:btnMenu];

When...