当作最后一篇吧,虽然还不能说是个游戏,但是,作为一个瞎玩的Demo感觉也可以了。
玩家信息类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(); } }
选关界面:
选关界面也是在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; } }