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

  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. L The Digits String(没有写完,有空补)

    链接:https://ac.nowcoder.com/acm/contest/338/L来源:牛客网 Consider digits strings with length n, how many d ...

  2. centos7配置sudo免密

    1.chmod  +w   /etc/sudoers 2.vim  /etc/sudoers 在已经有了的root下面加 username  ALL=NOPASSWD:ALL      (这是所有的命 ...

  3. rpmbuild - 构建 RPM 打包

    SYNOPSIS 构建打包: rpmbuild {-ba|-bb|-bp|-bc|-bi|-bl|-bs} [rpmbuild-options] SPECFILE ... rpmbuild {-ta| ...

  4. java ArrayList的几种方法使用

    package java06; import java.util.ArrayList; /* ArrayList的常用的几个方法: public boolean add(E e) : 向集合汇总添加元 ...

  5. CentOS 7.4 安装python3及虚拟环境

    [转]:https://www.centos.bz/2018/05/centos-7-4-%e5%ae%89%e8%a3%85python3%e5%8f%8a%e8%99%9a%e6%8b%9f%e7 ...

  6. jsp中引入js文件缓存问题解决

    加上版本UUID <script charset="utf-8" src="${basePath}js/souke/soukegalist.js?v=<%=U ...

  7. 01.springboot入门--启用自动配置注解EnableAutoConfiguration

    springboot入门 <parent> <groupId>org.springframework.boot</groupId> <artifactId&g ...

  8. cmd退出python

    cmd中如何退出Python (1)在命令行上输入exit() (2)在命令行上输入quit() (3)ctrl+Z 然后回车

  9. SpringBoot---Servlet容器(Tomcat)配置

    1.概述 1.1.Tomcat所有属性  都在org,springframework.boot.autoconfigure.web.ServerProperties配置类中作了定义: 2.替换Tomc ...

  10. 纯JSP简单登录实例

    记一下,免得以后忘记了,又要去查. 文件共有四个web.xml.login.jsp.logout.jsp.welcome.jsp四个文件 测试环境:Tomcat 6.0.x 假设项目名称是LoginS ...