Game Development Reference
In-Depth Information
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,
GameLay-
er::StartGame
.
2. Next, we add the tutorial button:
menuItemOn =
Sprite::createWithSpriteFrameName("btn_howto_on.png");
menuItemOff =
Sprite::createWithSpriteFrameName("btn_howto_off.png");
auto howToItem = MenuItemSprite::create( menuItemOff,
menuItemOn, CC_CALLBACK_1(GameLayer::showTutorial,
this));
3. Then it's time to create the menu: