用cocos2dx做一个简单的单机捕鱼达人游戏(1)
我使用了cocos2dx 3.9版本和vs2017来开发
今天先做游戏开始界面
开始界面很简单,一个背景图,一个logo,3个button(三种登录方式),一个背景音乐开关checkbox
游客登录按钮,点击以后调用切换场景的代码,我自己写了一个场景管理SceneManager类,
背景音乐按钮用了lambda表达式 ,背景音乐只有两种状态,进游戏默认播放背景音乐,点击以后停止播放背景音乐。
LoadingLayer.h的代码
#pragma once
#ifndef _LoadingLayer_H_
#define _LoadingLayer_H_
#include"cocos2d.h"
#include "uiCocosGUI.h"
USING_NS_CC;
using namespace ui;
class LoadingLayer:public Layer
{
public:
LoadingLayer();
~LoadingLayer();
static LoadingLayer*create();
virtual bool init() override;
private:
};
#endif // !_LoadingLayer_H_
LoadingLayer.cpp
#include"LoadingLayer.h"
#include"SceneManager.h"
// 导入头文件
#include "SimpleAudioEngine.h"
using namespace CocosDenshion;
LoadingLayer::LoadingLayer()
{
}
LoadingLayer::~LoadingLayer()
{
}
LoadingLayer * LoadingLayer::create()
{
// 二段构建--第一段分配内存
LoadingLayer* pLayer = new LoadingLayer;
// 第二段初始化
if (pLayer && pLayer->init())
{
// 加入自动释放对象池
pLayer->autorelease();
}
else
{
delete pLayer;
pLayer = nullptr;
}
return pLayer;
}
bool LoadingLayer::init()
{
// 先初始化父类
if (!Layer::init())
{
return false;
}
//背景图片添加
Sprite*BeiJing = Sprite::create("/loadinglayer/LoadingLayer.png");
//加到层上
this->addChild(BeiJing);
//设置图片的位置
BeiJing->setPosition(500,300);
//登录界面上方图片添加
Sprite*loginUP = Sprite::create("/loadinglayer/LogonUp.png");
this->addChild(loginUP);
loginUP->setPosition(525, 570);
//登录图片加入
// 游客登录按钮
Button*youkeLogin = Button::create("/loadinglayer/QuickLogon.png",
"/loadinglayer/QuickLogon.png",
"/loadinglayer/QuickLogon.png");
this->addChild(youkeLogin);
//位置设置
youkeLogin->setPosition(Vec2(200, 100));
//触摸事件响应
youkeLogin->addTouchEventListener([&](Ref* ref, Widget::TouchEventType type)
{
//切换场景(点击进入菜单界面)
Director::getInstance()->replaceScene(SceneManager::create(MENU_SCENE));
});
SimpleAudioEngine::getInstance()->playBackgroundMusic("bgMusic.mp3");
CheckBox* box = CheckBox::create("open.png", "close.png");
this->addChild(box);
box->setPosition(Vec2(950, 600));
box->setScale(0.6f);//设置缩放
box->addEventListener([&](Ref* ref,
CheckBox::EventType type)
{
switch (type)
{
case cocos2d::ui::CheckBox::EventType::SELECTED:
SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
break;
case cocos2d::ui::CheckBox::EventType::UNSELECTED:
SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
break;
default:
break;
}
});
Sprite*qqLogin = Sprite::create("/loadinglayer/QQLogon.png");
this->addChild(qqLogin);
qqLogin->setPosition(525, 100);
Sprite*wechatLogin = Sprite::create("/loadinglayer/WechatLogon.png");
this->addChild(wechatLogin);
wechatLogin->setPosition(850, 100);
return true;
}