cocos2d-x游戏开发系列教程-中国象棋03-主界面
来源:互联网 发布:淘宝省市区 api 编辑:程序博客网 时间:2024/04/30 02:45
前情回顾
上个博客说道我们象棋程序进入了欢迎界面,在欢迎界面下等待一秒进入主界面
进入主界面的关键代码如下:
CCScene* pScene = CCMainMenu::scene(); 创建scene
...
CCDirector::sharedDirector()->replaceScene(ps); 显示scene
最关键的是CCMainMenu::scene函数,说明后面要显示的scene类型是CCMainMenu类型的
所以接下来我们一起来看CCMainMenu是怎么回事
CCMainMenu
typedef struct{CCSprite* pSprite;float x; // coordinatefloat y;}SPRITE_INFO;typedef std::vector<SPRITE_INFO> VEC_SPRITE;typedef VEC_SPRITE::iterator VEC_SPRITE_IT;class CCMainMenu : public CCLayer{public:static CCScene* scene();virtual bool init();CREATE_FUNC(CCMainMenu);virtual void ccTouchesEnded(CCSet* pTouches, CCEvent* pEvent);private:voidsetPos(CCSprite* pSprite, int x, int y);voidsetPosDigit(CCSprite* pSprite, int x, int y);voidmenuCallBack(CCObject* pSender);voidmenuRegret(CCObject* pSender);voidmenuStart(CCObject* pSender);voidmenuHardChoose(CCObject* pSender);voidmenuSoundChoose(CCObject* pSender);voidupdateTime(float dt);voidupdateFocus(float dt);voidinitCoordinate();voidinitListImage();voidinitTimeShow();voidinitChessPosition();CCSprite*getNumberSprite(int nNum);voidsetNumberSprite(int nChessTime);CCSprite*getListSprite(CHESS_TYPE nType);CHESS_TYPE getChessType(int i, int j);voiddealWithChess(float x, float y);CCSprite*getChessByCoord(float x, float y);boolgetChessByCoord(float& x, float& y, short dt);voidcollectInfo(CCSprite* pSprite);booljudgeAction(int tx, int ty);voidfreeOne();voidfreeAll();booljudgeContinuous();voidclean();booljudgeWin();//voidprint();private:CCSprite*m_pFocus;CCSprite*m_pBKG;CCSprite*m_pMinute[2];CCSprite*m_pSecond[2];CCSprite*m_pCurChess;CCSprite* m_pTargetChess;CHESS_TYPE m_enCurChessType;CHESS_TYPE m_enTarChessType;CCSprite*m_pChess[2][16];CCMenuItemImage* m_pOpenVolice;CCMenuItemImage* m_pCloseVolice;//std::ofstream f_output;//std::string str;floatm_fPositionX;floatm_fPositionY;intm_nChessTime;CCSizes;boolm_bSelect; // has a chess be selected? if true, cannot be changeboolm_bVolice;boolm_bCollectCorrect;intox, oy; // the original coordinate of the current chessintm_nContinuous; // the max numbers to attact opponent's bossintm_nCur; // 0:red; 1:blackint m_nWin; // 0:red; 1:blackenum GAME_STATUS{GAME_MENU,GAME_RUNNING,GAME_WIN,GAME_OVER,};GAME_STATUS m_enGameStatus;enum GAME_ROLE{ROLE_RED,ROLE_BLACK,};GAME_ROLE m_enCurRole;VEC_SPRITE m_vecSprite;};oh,天,这个类太复杂了,对我们初学者来说,但是不要害怕,其他的我们先不管他是咋的,我们先来看基本的函数
一个是scene函数,一个是init函数。scene函数是创建函数,而init函数时初始化调用函数。
scene函数
CCScene* CCMainMenu::scene(){CCScene* pScene = CCScene::create();CCMainMenu* pLayer = CCMainMenu::create();pScene->addChild(pLayer, 3);return pScene;}scene函数和WelCome的scene函数类似,也是先创建一个scene,再创建一个CCMainMenu对象,然后把CCMainMenu对象加入到scene
最后返回scene
init函数
bool CCMainMenu::init(){// 调用父类init函数if (!CCLayer::init()){return false;}// 获取窗口大小,这个窗口大小将来用来计算控件位置等s = CCDirector::sharedDirector()->getWinSize();// 创建背景图片,并设置m_pBKG = CCSprite::create(RES_PATH"background.png");CCRect r = m_pBKG->getTextureRect();m_pBKG->setAnchorPoint(CCPointZero);m_pBKG->setPosition(ccp(0, 0));m_pBKG->setScaleX(s.width/r.size.width*0.667f);m_pBKG->setScaleY(s.height/r.size.height);this->addChild(m_pBKG, -2);// 创建焦点图片精灵,这个焦点是指棋子被选中m_pFocus = CCSprite::create(RES_PATH"selected.png");r = m_pFocus->getTextureRect();m_pFocus->setScaleX(0.667f);m_pFocus->setScaleY(0.6f);m_pFocus->setVisible(false);this->addChild(m_pFocus, 1);m_fPositionX = s.width - r.size.width*0.667f;m_fPositionY = s.height - r.size.height*0.6f;// 新局按钮CCMenuItemImage* pItem = CCMenuItemImage::create(RES_PATH"new.jpg", RES_PATH"new.jpg", this, menu_selector(CCMainMenu::menuCallBack));pItem->setPosition(ccp(m_fPositionX - s.width/6, m_fPositionY - s.height/8));pItem->setAnchorPoint(CCPointZero);pItem->setScaleX(0.667f);pItem->setScaleY(0.6f);CCMenu* pMenu = CCMenu::create(pItem, NULL);pMenu->setPosition(CCPointZero);this->addChild(pMenu, 1);// 悔棋按钮pItem = CCMenuItemImage::create(RES_PATH"regret.jpg", RES_PATH"regret.jpg", this, menu_selector(CCMainMenu::menuRegret));pItem->setPosition(ccp(m_fPositionX - s.width/6, m_fPositionY - s.height/8*2));pItem->setAnchorPoint(CCPointZero);pItem->setScaleX(0.667f);pItem->setScaleY(0.6f);pMenu = CCMenu::create(pItem, NULL);pMenu->setPosition(CCPointZero);this->addChild(pMenu, 1);// 开始按钮pItem = CCMenuItemImage::create(RES_PATH"start.jpg", RES_PATH"start.jpg", this, menu_selector(CCMainMenu::menuStart));pItem->setPosition(ccp(m_fPositionX - s.width/6, m_fPositionY - s.height/8*3));pItem->setAnchorPoint(CCPointZero);pItem->setScaleX(0.667f);pItem->setScaleY(0.6f);//pMenu = CCMenu::create(pItem, NULL); xueguoliangpMenu = CCMenu::create(pItem, NULL);pMenu->setPosition(CCPointZero);this->addChild(pMenu, 1);// 难度按钮pItem = CCMenuItemImage::create(RES_PATH"difficulty.jpg", RES_PATH"difficulty.jpg", this, menu_selector(CCMainMenu::menuHardChoose));pItem->setPosition(ccp(m_fPositionX - s.width/6, m_fPositionY - s.height/8*4));pItem->setAnchorPoint(CCPointZero);pItem->setScaleX(0.667f);pItem->setScaleY(0.6f);pMenu = CCMenu::create(pItem, NULL);pMenu->setPosition(CCPointZero);this->addChild(pMenu, 1);// 声音按钮m_pOpenVolice = CCMenuItemImage::create(RES_PATH"openVolice.png", RES_PATH"openVolice.png", this, menu_selector(CCMainMenu::menuSoundChoose));CCSize r2 = m_pOpenVolice->getContentSize();m_pOpenVolice->setPosition(ccp(m_fPositionX - s.width/6 + r.size.width/2 - r2.width/6, m_fPositionY - s.height/8*5));m_pOpenVolice->setAnchorPoint(CCPointZero);m_pOpenVolice->setScaleX(0.667f);m_pOpenVolice->setScaleY(0.6f);pMenu = CCMenu::create(m_pOpenVolice, NULL);pMenu->setPosition(CCPointZero);this->addChild(pMenu, 1, 100);// 关闭声音按钮m_pCloseVolice = CCMenuItemImage::create(RES_PATH"closeVolice.png", RES_PATH"closeVolice.png", this, menu_selector(CCMainMenu::menuSoundChoose));m_pCloseVolice->setPosition(ccp(m_fPositionX - s.width/6 + r.size.width/2 - r2.width/6, m_fPositionY - s.height/8*5));m_pCloseVolice->setAnchorPoint(CCPointZero);m_pCloseVolice->setScaleX(0.667f);m_pCloseVolice->setScaleY(0.6f);pMenu = CCMenu::create(m_pCloseVolice, NULL);pMenu->setPosition(CCPointZero);m_pCloseVolice->setVisible(false);this->addChild(pMenu, 1, 101);// 坐标信息this->initCoordinate();// 初始化时间this->initTimeShow();// 初始化图片this->initListImage();this->setTouchEnabled(true);this->schedule(schedule_selector(CCMainMenu::updateTime), 1.0f);// 这个相当于做一个双buffer,移动一个buffer,显示一个bufferfor (int i = 0; i < 10; ++i){for (int j = 0; j < 9; ++j){g_cur_map[i][j] = g_chess_map[i][j];}}// 一些游戏中用到的状态变量,需要的时候我们再详细描述m_enGameStatus= GAME_MENU;m_enCurRole= ROLE_RED;m_bVolice= true;m_pCurChess= NULL;m_bSelect= false;m_bCollectCorrect= true;m_pTargetChess= NULL;m_nContinuous= 3;m_nCur= 0;m_enCurChessType= CHESS_NONE;m_enTarChessType = CHESS_NONE;m_nWin= -1;m_vecSprite.reserve(10);return true;}init函数执行完之后,程序就显示出主界面如下:
所以主界面其实含有摆棋子的功能,除了摆棋子之外,还需要准备控制控件和状态量等等,这些工作
都是在init函数中完成,理解了init函数,也就理解了大部分象棋程序的数据结构了。
在init函数主干代码上,大部分代码完成了右边空间创建和状态量初始化,而棋子位置初始化,则调用
如下函数中实现。
// 坐标信息
this->initCoordinate();
// 初始化图片
this->initListImage();
0 0
- cocos2d-x游戏开发系列教程-中国象棋03-主界面
- cocos2d-x游戏开发系列教程-中国象棋05-开始游戏
- cocos2d-x游戏开发系列教程-中国象棋00-前言
- cocos2d-x游戏开发系列教程-中国象棋04-摆棋
- cocos2d-x游戏开发系列教程-中国象棋06-游戏规则
- cocos2d-x游戏开发系列教程-中国象棋01-工程文件概述
- cocos2d-x游戏开发系列教程-中国象棋02-main函数和欢迎页面
- cocos2d-x游戏开发系列教程-坦克大战游戏启动界面的编写
- cocos2d-x游戏开发系列教程-前言
- Cocos2d-X开发中国象棋《四》设计游戏场景
- Cocos2d-X开发中国象棋《六》游戏开始功能的实现
- Cocos2d-x开发中国象棋《十一》在游戏中添加背景音乐
- Cocos2d-X开发中国象棋《十四》游戏打包成APK
- cocos2d-x游戏开发系列教程-超级玛丽01-前言
- cocos2d-x游戏开发系列教程-超级玛丽04-AppDelegate
- cocos2d-x游戏开发系列教程-超级玛丽05-CMMenuScene
- cocos2d-x游戏开发系列教程-超级玛丽06-CMGameScene
- cocos2d-x游戏开发系列教程-超级玛丽07-CMGameMap
- 栈
- python使用rabbitmq实例六,远程结果返回(6)
- 指针(2)指针常见问题(re迭代)
- 函数指针与指针函数
- python使用rabbitmq实例七,相互关联编号correlation id(7)
- cocos2d-x游戏开发系列教程-中国象棋03-主界面
- RMI理解
- 对esper的理解
- atoi 把字符串转换成相应的整形数
- 用汇编实现数字转化为字符串的函数itoa
- Cocos2d-html5入门笔记(一)
- Trie树入门:HDU 1671
- 易控报警组态学习
- 在Eclipse里自动导入相应包的类名的快捷键