Book Image

Cocos2d Game Development Essentials

Book Image

Cocos2d Game Development Essentials

Overview of this book

Table of Contents (13 chapters)

Adding buttons to your scene


Buttons are a requirement for almost any app. You will have to use them constantly in your game development. Cocos2d v3 makes this very simple with the introduction of the new class, CCButton. This is a great improvement over version 2 for those familiar with it. Making buttons is now as easy as making any other node. It is even better as they support block-based callbacks or target/selector calls that gives you the flexibility to work with buttons however you like.

Buttons have three states:

  • Default

  • Selected

  • Disabled

If you open up IntroScene.m in your Xcode project, you will see an example of a button being created:

    // Helloworld scene button
    CCButton *helloWorldButton = [CCButton buttonWithTitle:@"[ Start ]" fontName:@"Verdana-Bold" fontSize:18.0f];
    helloWorldButton.positionType = CCPositionTypeNormalized;
    helloWorldButton.position = ccp(0.5f, 0.35f);
    [helloWorldButton setTarget:self selector:@selector(onSpinningClicked:)];
    [self addChild...