上一课我们主要给游戏增加了一个暂停功能,具体说就是游戏层中右上角添加一个按钮,用于暂停游戏功能的实现。本篇创建游戏开始界面,用于选择开始游戏还是退出游戏。

开发环境

Win64 : vs2010

Cocos2d-x v3.4Final

TexturePackerGUI

MapEdit

简单的做一个开始界面.

我们需要:

背景

按钮

开始界面的背景,本例中使用一张已经做好的图片作为背景.

按钮放上3个,分别是:

开始游戏

游戏设置

结束游戏

回调Scene来处理它们

代码

other

GameStart

.h

#ifndef _GAME_START_SCENE_H_#define _GAME_START_SCENE_H_#include USING_NS_CC;

//#include "LevelChooseLayer.h"#include "GameScene.h"#include "ui/CocosGUI.h"#include "BarrierLayer.h"using namespace ui;class GameStartScene :public Layer{

public:

static cocos2d::Scene* createScene();

virtual bool init();

void StartGameCallBack(Ref *pSender , Widget::TouchEventType type);

void SettingCallBack(Ref *pSender , Widget::TouchEventType type);

void CloseGameCallBack(Ref *pSender , Widget::TouchEventType type); CREATE_FUNC(GameStartScen`

);

};

#endif

.cpp

#include "GameStartScene.h"#include "PopupLayer.h"Scene* GameStartScene::createScene()

{

auto scene = Scene::create();

auto Layer = GameStartScene::create();

scene->addChild(Layer);

return scene;

}bool GameStartScene ::init()

{

bool ret = false; do { Size visibleSize = Director::getInstance()->getVisibleSize();

auto blackground = Sprite::create("blackground.png");

addChild(blackground);

blackground->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2));

auto start_button = Button::create("button.png");

start_button->setTitleText("Start");

start_button->setTitleFontName("微软雅黑");

start_button->setTitleFontSize(16);

start_button->setPosition(Vec2(visibleSize.width/2,visibleSize.height*0.75));

start_button->addTouchEventListener(CC_CALLBACK_2(GameStartScene::StartGameCallBack,this));

addChild(start_button);

auto setting_button = Button::create("button.png");

setting_button->setTitleText("Set");

setting_button->setTitleFontName("微软雅黑");

setting_button->setTitleFontSize(16);

setting_button->setPosition(Vec2(visibleSize.width/2,visibleSize.height*0.5));

setting_button->addTouchEventListener(CC_CALLBACK_2(GameStartScene::SettingCallBack,this));

addChild(setting_button);

auto close_button = Button::create("button.png");

close_button->setTitleText("End");

close_button->setTitleFontName("微软雅黑");

close_button->setTitleFontSize(16);

close_button->setPosition(Vec2(visibleSize.width/2,visibleSize.height*0.25));

close_button->addTouchEventListener([=](Ref* pSender , Widget::TouchEventType type)

{

switch (type)

{ case cocos2d::ui::Widget::TouchEventType::ENDED: Director::getInstance()->end();

break;

}

});

addChild(close_button);

ret = true;

} while(0);

return ret;

}void GameStartScene::StartGameCallBack(Ref *pSender , Widget::TouchEventType type){ CCLOG("StartGame");

switch (type)

{ case cocos2d::ui::Widget::TouchEventType::ENDED: Director::getInstance()->replaceScene(TransitionCrossFade::create(0.5f,GameScene::createScene()));

break;

}

}void GameStartScene::SettingCallBack(Ref *pSender , Widget::TouchEventType type){

}void GameStartScene::CloseGameCallBack(Ref *pSender , Widget::TouchEventType type){

switch (type)

{ case cocos2d::ui::Widget::TouchEventType::ENDED: Director::getInstance()->end();

break;

}

}

需要注意的:

Director::getInstance()->end();

结束整个游戏

而下面这2段代码功能相同,只是一个是直接在后面添加并编写当前回调函数,另一个是回调已经写好的函数

1close_button->addTouchEventListener([=](Ref* pSender , Widget::TouchEventType type) { switch (type) { case cocos2d::ui::Widget::TouchEventType::ENDED: Director::getInstance()->end(); break; } });

以下代码实际上并未使用

void GameStartScene::CloseGameCallBack(Ref *pSender , Widget::TouchEventType type)

{

switch (type)

{ case cocos2d::ui::Widget::TouchEventType::ENDED:

Director::getInstance()->end();

break;

}

}

这一句调用已经写好的Scene切换动画来实现切换

1Director::getInstance()->replaceScene(TransitionCrossFade::create(0.5f,GameScene::createScene()));

最后特比特别注意:

switch (type)

本人在使用过程中忘了添加按键判定,由于Button点击一次有2种状态,Began和Ended.

导致在点击一次的情况下函数调用了2次,这种低级错误卡了我半个小时.

望各位特别关注,Button不是Menu,自己写回调代码需要注意.

AppDelegate

当游戏刚开始,希望载入的是GameStartScene而不是GameScene,需要在AppDelegate中修改它,当然最上方需要包含GameStartScene

- .cpp

#include "GameStartScene.h"...

auto scene = GameStartScene::createScene();

director->runWithScene(scene);

结果


1.gif


2.gif

结语

         本篇非常简单的创建了一个开始界面,下篇通过ScrollView创建关卡选择界面。(为什么不用PageView?需要触摸回弹并利用ScrollView动画回弹效果)