Book Image

Cocos2d-x by Example: Beginner's Guide

By : Roger Engelbert
Book Image

Cocos2d-x by Example: Beginner's Guide

By: Roger Engelbert

Overview of this book

Table of Contents (19 chapters)
Cocos2d-x by Example Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – creating Menu and MenuItem


In GameLayer.cpp, scroll down to the createGameScreen method. We'll add the new logic to the end of this method.

  1. First, create the menu item for our start game button:

    auto menuItemOn =  Sprite::createWithSpriteFrameName("btn_new_on.png");
    auto menuItemOff =  Sprite::createWithSpriteFrameName("btn_new_off.png");
    
    auto starGametItem = MenuItemSprite::create( menuItemOff,
    menuItemOn, CC_CALLBACK_1(GameLayer::startGame, this));

    We create a MenuItemSprite object by passing it one sprite per state of the button. When the user touches a MenuItemSprite object, the off state sprite is turned invisible and the on state sprite is turned visible, all inside the touch began event. If the touch is ended or cancelled, the off state is displayed once again.

    We also pass the callback function for this item; in this case, GameLayer::StartGame.

  2. Next, we add the tutorial button:

    menuItemOn =  Sprite::createWithSpriteFrameName("btn_howto_on.png");
    menuItemOff =  Sprite...