|   版权声明:本文为博主原创文章,未经博主允许不得转载。

  MainMenu类主要实现的是游戏主界面的布局,它相当于一个港口,有开向各处的航道,而游戏中的MainMenu则是有跳转到各个场景的一个集合点。

  下面贴上代码:

MainMenu.h

#ifndef _MAIN_MENU_H_
#define _MAIN_MENU_H_ ////////////////////////////////////////////////////////////////
// 这里主要的是实现主界面的一些操作
#include "cocos2d.h"
USING_NS_CC; class MainMenu : public cocos2d::Layer
{
private:
cocos2d::Sprite* background;
cocos2d::MenuItemImage* playItem;
cocos2d::MenuItemImage* scoreItem;
cocos2d::MenuItemImage* aboutItem;
cocos2d::MenuItemImage* exitItem;
cocos2d::Sprite* logo;
cocos2d::Sprite* bird;
cocos2d::Sprite* land;
public:
static cocos2d::Scene* createScene();
virtual bool init();
void interfaceLayout();
void goPlay(cocos2d::Ref*);
void goScore(cocos2d::Ref*);
void goAbout(cocos2d::Ref*);
void goExit();
CREATE_FUNC(MainMenu);
}; #endif // _MAIN_MENU_H_

MainMenu.cpp

#include "MainMenu.h"
#include "GameUnit.h"
#include "GameAbout.h"
#include "GameScore.h"
#include "GamePlay.h"
#include "GameData.h" unit u; //创建一个游戏场景
cocos2d::Scene* MainMenu::createScene()
{
auto scene = Scene::create();
auto layer = MainMenu::create();
scene->addChild(layer);
return scene;
} //初始化
bool MainMenu::init()
{
if (!Layer::init())
{
return false;
} //设置主界面的背景音乐 //初始化游戏数据
if (!GameData::getGameData())
{
GameData::initGameData();
}
//布局主界面
this->interfaceLayout(); return true;
} //游戏界面布局
void MainMenu::interfaceLayout()
{
//1: add background
background = Sprite::create("background/light.png");
background->setPosition(Vec2(u.winOrigin().x + u.winSize().width / 2,
u.winOrigin().y + u.winSize().height / 2));
background->setScale(u.scaleX(background, u.winSize()),
u.scaleY(background, u.winSize()));
this->addChild(background, 0); //2: add FlyBird logo
logo = Sprite::create("logo/title.png");
logo->setPosition(Vec2(u.winOrigin().x + u.winSize().width / 2,
u.winOrigin().y + u.winSize().height - 2 * logo->getContentSize().height));
logo->setScale(1.5);
this->addChild(logo, 1); //3: add wall
land = Sprite::create("background/land.png");
land->setPosition(Vec2(u.winOrigin().x + u.winSize().width / 2,
u.winOrigin().y + land->getContentSize().height / 2));
land->setScaleX(u.winSize().width / 1.5*land->getContentSize().width);
//land->setScaleY(1.5*u3.winSize().height / backgroundA->getContentSize().height);
land->setScaleY(u.winSize().height / (land->getContentSize().height * 3));
this->addChild(land); //4: add bird animation
bird = Sprite::create("bird/littleBird.png");
bird->setPosition(Vec2(u.winOrigin().x + u.winSize().width / 2,
u.winOrigin().y + (u.winSize().height / 3) * 2));
bird->setScale(2);
auto repeat = RepeatForever::create(Animate::create(u.gameAnimate(1)));
bird->runAction(repeat);
this->addChild(bird); //5: add about button
aboutItem = MenuItemImage::create(
"button/about.png",
"button/buttom.png",
CC_CALLBACK_1(MainMenu::goAbout, this));
aboutItem->setPosition(Vec2(u.winOrigin().x + u.winSize().width - aboutItem->getContentSize().width / 2,
u.winOrigin().y + aboutItem->getContentSize().height / 2));
auto m = Menu::create(aboutItem, NULL);
m->setPosition(Vec2::ZERO);
this->addChild(m, 2); //6: add play button
playItem = MenuItemImage::create(
"button/play.png",
"button/play.png",
CC_CALLBACK_1(MainMenu::goPlay, this)
); //7: add score button
scoreItem = MenuItemImage::create(
"button/score.png",
"button/score.png",
CC_CALLBACK_1(MainMenu::goScore, this)
); auto menu = Menu::create(playItem, scoreItem, NULL);
menu->alignItemsHorizontallyWithPadding(10);
menu->setPosition(u.winOrigin().x + u.winSize().width - menu->getContentSize().width/4,
u.winOrigin().y + 1.7*menu->getContentSize().height/3);
menu->setScale(1.5);
this->addChild(menu, 2); //exit
exitItem = MenuItemImage::create(
"button/exit_f.png",
"button/exit_b.png",
CC_CALLBACK_0(MainMenu::goExit, this)
);
Menu* exitMenu = Menu::create(exitItem, NULL);
exitMenu->setPosition(Vec2(u.winOrigin().x + exitItem->getContentSize().width / 2,
u.winOrigin().y + exitItem->getContentSize().height / 2));
this->addChild(exitMenu, 7);
} void MainMenu::goPlay(cocos2d::Ref* pSender)
{
Director::getInstance()->replaceScene(TransitionFadeTR::create(1,
GamePlay::createScene()));
} void MainMenu::goScore(cocos2d::Ref* pSender)
{
Director::getInstance()->replaceScene(TransitionFadeTR::create(1,
GameScore::createScene()));
} void MainMenu::goAbout(cocos2d::Ref* pSender)
{
Director::getInstance()->replaceScene(TransitionFadeTR::create(1,
GameAbout::createScene()));
} void MainMenu::goExit()
{
Director::getInstance()->end();
}

每个函数的功能见Cocos2d之FlyBird开发---简介

效果图:

Cocos2d 之FlyBird开发---MainMenu类-LMLPHP

05-21 14:05