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

  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类的更多相关文章

  1. Cocos2d 之FlyBird开发---GameUnit类

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. 这节来实现GameUnit类中的一些函数方法,其实这个类一般是一个边写边完善的过程,因为一般很难一次性想全所有的能够供多个类共用的方法.下 ...

  2. Cocos2d 之FlyBird开发---GameScore类

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. 这个类主要实现的是,显示历次成绩中的最好成绩.当然我写的这个很简洁,还可以写的更加的丰富.下面贴上代码: GameScore.h #ifn ...

  3. Cocos2d 之FlyBird开发---GameData类

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. 现在是大数据的时代,绝大多数的游戏也都离不开游戏数据的控制,简单的就是一般记录游戏的得分情况,高端大气上档次一点的就是记录和保存各方面的游 ...

  4. Cocos2d 之FlyBird开发---GamePlay类

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. 这个是游戏的核心部分:(FlyBird游戏重中之重) 创建一个物理世界(世界设置重力加速度) 在物理世界中添加一个动态的刚体(小鸟) 在物 ...

  5. Cocos2d 之FlyBird开发---GameAbout类

    |   版权声明:本文为博主原创文章,未经博主允许不得转载.(笔者才疏学浅,如有错误,请多多指教) 一般像游戏关于的这种界面中,主要显示的是游戏的玩法等. GameAbout.h #ifndef _G ...

  6. Cocos2d之FlyBird开发---简介

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. 开发FlyBird其实非常的简单,在游戏的核心部分,我们需要实现的只有: 创建一个物理世界(世界设置重力加速度) 在物理世界中添加一个动态 ...

  7. JAVA串口开发帮助类分享-及写在马年末

    摘要: 在系统集成开发过程中,存在着各式的传输途径,其中串口经常因其安全性高获得了数据安全传输的重用,通过串口传输可以从硬件上保证数据传输的单向性,这是其它介质所不具备的物理条件.下面我就串口java ...

  8. iOS cocos2d 2游戏开发实战(第3版)书评

    2013是游戏爆发的一年,手游用户也是飞速暴增.虽然自己不做游戏,但也是时刻了解手机应用开发的新动向.看到CSDN的"写书评得技术图书赢下载分"活动,就申请了一本<iOS c ...

  9. (转载)实例详解Android快速开发工具类总结

    实例详解Android快速开发工具类总结 作者:LiJinlun 字体:[增加 减小] 类型:转载 时间:2016-01-24我要评论 这篇文章主要介绍了实例详解Android快速开发工具类总结的相关 ...

随机推荐

  1. Introduction to Sound Programming with ALSA

    ALSA stands for the Advanced Linux Sound Architecture. It consists of a set of kernel drivers, an ap ...

  2. 125-FMC125-两路125Msps AD,两路160Msps DA FMC子卡模块

    FMC125-两路125Msps AD,两路160Msps DA FMC子卡模块 1.板卡概述  该板卡可实现2路14bit 250Msps AD 和2路16bit 160MspsDA功能,FMC连接 ...

  3. 【LeetCode】一种博弈思路 minimax(共5题)

    [292] Nim Game (2019年3月12日,E) 有一堆石头,游戏规则是每次可以从里面拿1-3颗石头,拿到最后的石头的人赢.你和你的对手都 optimal 的玩这个游戏,问先手(也就是你)能 ...

  4. count(1)、count(*)、count(字段)的区别

    count(1)和count(*): 都为统计所有记录数,包括null 执行效率上:当数据量1W+时count(*)用时较少,1w以内count(1)用时较少 count(字段): 统计字段列的行数, ...

  5. LOJ6252. 「CodePlus 2017 11 月赛」大吉大利,晚上吃鸡! 最短路+bitset

    题目传送门 https://loj.ac/problem/6252 https://lydsy.com/JudgeOnline/problem.php?id=5109 题解 首先跑最短路,只保留 \( ...

  6. BZOJ4710 [Jsoi2011]分特产 容斥

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4710 题解 本来想去找一个二项式反演的题的,结果被 https://www.cnblogs.c ...

  7. BZOJ3227 [sdoi2008]红黑树

    贪心什么的太神仙了( 老老实实dp于是就是沙茶题了 f[i][d][0/1]表示i个节点bh为d当前节点颜色白/黑[好好读题是真.. 转移一下然后就可以打表了( 由于我们发现这玩意很好卡有很好的性质( ...

  8. Java programming language does not use call by reference for objects!

    Instead, object references are passed by value! A method cannot modify a parameter of a primitive ty ...

  9. hdu 1506:Largest Rectangle in a Histogram 【单调栈】

    题目链接 对栈的一种灵活运用吧算是,希望我的注释写的足够清晰.. #include<bits/stdc++.h> using namespace std; typedef long lon ...

  10. ubuntu下安装pyaudio

    首先安装依赖库,不安装依赖库会安装失败: sudo apt-get install libasound-dev portaudio19-dev libportaudio2 libportaudiocp ...