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.
- 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 aMenuItemSprite
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
. - Next, we add the tutorial button:
menuItemOn = Sprite::createWithSpriteFrameName("btn_howto_on...