用cocos2dx做一个简单的单机捕鱼达人游戏(2)

第二步菜单界面创建
通过游客模式登录以后进入到菜单选择界面

3个大一点的button对应3种游戏模式,还有一个快速开始button,以及一个退出到loading界面的button,一个商城,一个图鉴,一个积分榜
menuLayer.h

#pragma once
#ifndef _MenuLayer_H_
#define _MenuLayer_H_
#include"cocos2d.h"
#include "ui\CocosGUI.h"


USING_NS_CC;
using namespace ui;
class MenuLayer:public Layer
{
public:
	MenuLayer();
	~MenuLayer();
	static MenuLayer*create();
	virtual bool init() override;
private:

};
#endif // !_MenuLayer_H_

menuLayer.cpp

#include"MenuLayer.h"
#include"SceneManager.h"

MenuLayer::MenuLayer()
{
	
}

MenuLayer::~MenuLayer()
{
}

MenuLayer * MenuLayer::create()
{
	// 二段构建--第一段分配内存
	MenuLayer* pLayer = new MenuLayer;
	// 第二段初始化
	if (pLayer && pLayer->init())
	{
		// 加入自动释放对象池
		pLayer->autorelease();
	}
	else
	{
		delete pLayer;
		pLayer = nullptr;
	}
	return pLayer;
}

bool MenuLayer::init()
{
	// 先初始化父类
	if (!Layer::init())
	{
		return false;
	}

	//背景图添加
	Sprite*menuBeiJing = Sprite::create("/menulayer/MenuBeiJing.png");
	this->addChild(menuBeiJing);
	menuBeiJing->setPosition(568,320);
	//3个大厅图片添加

	// 新手场登录按钮
	Button*newcomer = Button::create("/menulayer/fish_review_junior_lobby.png",
		"/menulayer/fish_review_junior_lobby.png",
		"/menulayer/fish_review_junior_lobby.png");
	this->addChild(newcomer);
	//位置设置
	newcomer->setPosition(Vec2(150, 300));
	//触摸事件响应
	newcomer->addTouchEventListener([&](Ref* ref, Widget::TouchEventType type)
	{
		//切换场景(点击进入游戏界面)
		Director::getInstance()->replaceScene(SceneManager::create(GAME_SCENE ));

	});
	//退出按钮
		Button*back = Button::create("Exit_Normal.png",
			"Exit_Normal.png",
			"Exit_Normal.png");
	this->addChild(back);
	back->setScale(0.7f);//设置缩放

	//位置设置
	back->setPosition(Vec2(950, 600));
	//触摸事件响应
	back->addTouchEventListener([&](Ref* ref, Widget::TouchEventType type)
	{
		//切换场景(点击进入游戏界面)
		Director::getInstance()->replaceScene(SceneManager::create(LOADING_SCENE));

	});

	Sprite*jingdian = Sprite::create("/menulayer/fish_review_medium_lobby.png");
	this->addChild(jingdian);
	jingdian->setPosition(520, 300);
	Sprite*longgong = Sprite::create("/menulayer/fish_review_senior_lobby.png");
	this->addChild(longgong);
	longgong->setPosition(880, 300);

	//快速开始按钮添加
	Button*quickGame = Button::create("quick_start.png",
		"quick_start.png",
		"quick_start.png");
	quickGame->setScale(0.7f);//设置缩放
	this->addChild(quickGame);
	//位置设置
	quickGame->setPosition(Vec2(900, 30));
	//触摸事件响应
	quickGame->addTouchEventListener([&](Ref* ref, Widget::TouchEventType type)
	{
		//切换场景(点击进入游戏界面)
		Director::getInstance()->replaceScene(SceneManager::create(GAME_SCENE));

	});


	//图鉴按钮添加
	Button*tujian = Button::create("tujian.png",
		"tujian.png",
		"tujian.png");
	
	this->addChild(tujian);
	//位置设置
	tujian->setPosition(Vec2(880, 600));
	//触摸事件响应
	tujian->addTouchEventListener([&](Ref* ref, Widget::TouchEventType type)
	{
		//切换场景(点击进入游戏界面)
		Director::getInstance()->replaceScene(SceneManager::create(TUJIAN_SCENE));

	});
		//商店按钮添加
		Button*shop = Button::create("fish_review_shop.png",
			"fish_review_shop.png",
			"fish_review_shop.png");

	this->addChild(shop);
	//位置设置
	shop->setPosition(Vec2(810, 600));
	//触摸事件响应
	shop->addTouchEventListener([&](Ref* ref, Widget::TouchEventType type)
	{
		//切换场景(点击进入游戏界面)
		Director::getInstance()->replaceScene(SceneManager::create(SHOP_SCENE));

	});
	//排行榜按钮添加
	Button*paihang = Button::create("phbbtn3_0.png",
		"phbbtn3_0.png",
		"phbbtn3_0.png");

	this->addChild(paihang);
	//位置设置
	paihang->setPosition(Vec2(710, 600));
	paihang->setScale(0.5f);//设置缩放
	//触摸事件响应
	paihang->addTouchEventListener([&](Ref* ref, Widget::TouchEventType type)
	{
		//切换场景(点击进入游戏界面)
		Director::getInstance()->replaceScene(SceneManager::create(PAIHANG_SCENE));

	});
	return true;
}

你可能感兴趣的:(cocos2dx,捕鱼达人,cocos2dx)