当作最后一篇吧,虽然还不能说是个游戏,但是,作为一个瞎玩的Demo感觉也可以了。
@H_404_10@玩家信息类Profile:
Profile设计为一个单例类,在游戏中主要负责保存和读取玩家的信息,其实,Demo中没有什么好保存的信息,但是,在选关的时候需要把选关的ID保存下来,在GameLayer中使用,所以,暂时添加了这个类。
.h
#ifndef _PROfile_H_#define _PROfile_H_#include "cocos2d.h"class Profile : public cocos2d::Ref{public: static Profile * s_pProfile; static Profile * getInstance(); static bool initStatic(); static voID finalizeStatic();public: Profile(); ~Profile(); //@biref 选中的关卡的ID CC_SYNTHESIZE(int,m_seleLevelID,SeleLevelID);private:};#endif
.cpp
#include "Profile.h"Profile * Profile::s_pProfile = nullptr;Profile * Profile::getInstance(){ if (!s_pProfile) { s_pProfile = new Profile(); } return s_pProfile;}bool Profile::initStatic(){ s_pProfile = new Profile(); return true;}voID Profile::finalizeStatic(){ CC_SAFE_RELEASE_NulL(s_pProfile);}Profile::Profile() :m_seleLevelID(-1){ m_seleLevelID = 0;//<默认选中的关切ID为0}Profile::~Profile(){}
之前没有用过cocostudio做界面,通过这个项目也学习下cocostudio的一些使用方法,菜单界面和选关界面就用cocostudio做了。
用cocostudio拼出了界面,然后在程序中使用,并添加了开始按钮和退出按钮的点击回调。
Menulayer.h
#ifndef _MENU_LAYER_H_#define _MENU_LAYER_H_#include "cocos2d.h"#include "ui/CocosGUI.h"class Menulayer : cocos2d::Layer{public: static const int button_TAG_START = 10; static const int button_TAG_CLOSE = 20; static cocos2d::Scene* createScene(); CREATE_FUNC(Menulayer);public: Menulayer(); ~Menulayer(); virtual bool init(); voID buttonClickCallback(cocos2d::Ref *pSender,cocos2d::ui::Widget::touchEventType type); private:};#endif
Menulayer.cpp
#include "Menulayer.h"#include "cocostudio/CocoStudio.h"#include "LevelScene.h"using namespace cocostudio::timeline;using namespace cocos2d::ui;USING_NS_CC;Scene* Menulayer::createScene(){ auto scene = Scene::create(); auto layer = Menulayer::create(); scene->addChild(layer); return scene;}Menulayer::Menulayer(){}Menulayer::~Menulayer(){}bool Menulayer::init(){ Layer::init(); Node * rootNode = csloader::createNode("Menulayer.csb"); this->addChild(rootNode); auto buttonStart = dynamic_cast<button*>(rootNode->getChildByname("button_start")); buttonStart->addtouchEventListener(CC_CALLBACK_2(Menulayer::buttonClickCallback,this)); buttonStart->setTag(button_TAG_START); auto buttonClose = button::create("Closenormal.png","CloseSelected.png"); buttonClose->setposition(Vec2(20,20)); rootNode->addChild(buttonClose); buttonClose->addtouchEventListener(CC_CALLBACK_2(Menulayer::buttonClickCallback,this)); buttonClose->setTag(button_TAG_CLOSE); return true;}voID Menulayer::buttonClickCallback(Ref *pSender,Widget::touchEventType type){ log("button clicked"); Node * pNode = dynamic_cast<Node*>(pSender); if (pNode->getTag() == button_TAG_START) { Director::getInstance()->replaceScene(LevelScene::createScene()); } else if (pNode->getTag() == button_TAG_CLOSE) { Director::getInstance()->end(); }}
@H_404_10@选关界面:
选关界面也是在cocostudio中做的,用到了ListvVIEw,添加了ListVIEw的点击回调,根据点击的Cell设置关卡的ID,进入游戏界面
LevelScene.h
#ifndef _MY_SCENE_H_#define _MY_SCENE_H_#include "cocos2d.h"#include "ui/CocosGUI.h"class LevelScene : public cocos2d::Scene{public: static Scene * createScene();public: LevelScene(); ~LevelScene(); virtual bool initSelf(); voID selectedItemEvent(cocos2d::Ref* pSender,cocos2d::ui::ListVIEw::EventType type); voID selectedItemEventScrollVIEw(cocos2d::Ref* pSender,cocos2d::ui::ScrollVIEw::EventType type);private:};
LevelScene.cpp
#include "LevelScene.h"#include "cocostudio/CocoStudio.h"#include "data/Profile.h"#include "GameLayer.h"using namespace cocostudio::timeline;using namespace cocos2d::ui;USING_NS_CC;Scene * LevelScene::createScene(){ auto scene = new LevelScene(); scene->initSelf(); scene->autorelease(); return scene;}LevelScene::LevelScene(){}LevelScene::~LevelScene(){}bool LevelScene::initSelf(){ Node * rootNode = csloader::createNode("MainScene.csb"); this->addChild(rootNode); auto node = dynamic_cast<Layout*>(rootNode->getChildByname("BG")); auto ListVIEw = dynamic_cast<ListVIEw*>(node->getChildByname("ListVIEw_3")); ListVIEw->addEventListener((ListVIEw::ccListVIEwCallback)CC_CALLBACK_2(LevelScene::selectedItemEvent,this)); //下面这个回调函数是不用的,只是为了测试 ListVIEw->addEventListener((ListVIEw::ccScrollVIEwCallback)CC_CALLBACK_2(LevelScene::selectedItemEventScrollVIEw,this)); return true;}voID LevelScene::selectedItemEvent(Ref* pSender,ListVIEw::EventType type){ switch (type) { case ListVIEw::EventType::ON_SELECTED_ITEM_START: { ListVIEw* ListVIEw = static_cast<ListVIEw*>(pSender); CC_UNUSED_ParaM(ListVIEw); //cclOG("select child start index = %ld",ListVIEw->getCurSelectedindex()); break; } case ListVIEw::EventType::ON_SELECTED_ITEM_END: { ListVIEw* ListVIEw = static_cast<ListVIEw*>(pSender); CC_UNUSED_ParaM(ListVIEw); //cclOG("select child end index = %ld",ListVIEw->getCurSelectedindex()); Profile::getInstance()->setSeleLevelID(ListVIEw->getCurSelectedindex()); log("LevelID:%d",Profile::getInstance()->getSeleLevelID()); //切换界面 Director::getInstance()->replaceScene(GameLayer::createScene()); break; } default: break; }}voID LevelScene::selectedItemEventScrollVIEw(Ref* pSender,ui::ScrollVIEw::EventType type){ switch (type) { case ScrollVIEw::EventType::SCRolL_TO_BottOM: //cclOG("SCRolL_TO_BottOM"); break; case ScrollVIEw::EventType::SCRolL_TO_top: //cclOG("SCRolL_TO_top"); break; default: break; }}
总结
以上是内存溢出为你收集整理的华容道08之菜单界面和选关界面的添加全部内容,希望文章能够帮你解决华容道08之菜单界面和选关界面的添加所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)