Cocos2d-x 捕鱼达人游戏学习教程(8)--添加PanelLayer数据面板层

时间:2023-02-08 10:45:52


代码下载 http://vdisk.weibo.com/s/BDn59yfnBV9dj

我们要在GameScene.h中添加如下代码:

 //数据面板层
CC_SYNTHESIZE_RETAIN(PanelLayer*, _panelLayer, PaneLayer);

//菜单页面相关的函数
//暂定
void pause();



之后我们要完成PanelLayer.h

#include "cocos2d.h"
USING_NS_CC;

class PanelLayer:public CCLayer
{

public:
CREATE_FUNC(PanelLayer);
bool init();
void pause();
};

PanelLayer.cpp

#include "GameScene.h"
#include "StaticData.h"

bool PanelLayer::init()
{
bool pRet = false;
do {
CC_BREAK_IF(!CCLayer::init());
CCSize winSize = CCDirector::sharedDirector()->getWinSize();

CCMenuItemSprite* pause = CCMenuItemSprite::create(CCSprite::createWithSpriteFrameName(STATIC_DATA_STRING("pause_normal")), CCSprite::createWithSpriteFrameName(STATIC_DATA_STRING("pause_selected")), this, menu_selector(PanelLayer::pause));

CCMenu* menu = CCMenu::create(pause,NULL);
this->addChild(menu);

CCSize pauseSize = pause->getContentSize();
menu->setPosition(ccp(winSize.width - pauseSize.width * 0.5, pauseSize.height * 0.5));

pRet = true;
} while (0);
return pRet;
}

void PanelLayer::pause()
{
//getParent()方法找到当前层的父类,当前层是加在GameScene中的因此
GameScene* gameScene = (GameScene*)this->getParent();
gameScene->pause();

}




GameScene.cpp的  bool GameScene ::init()方法中添加如下代码

 //创建显示数据层并加到场景中
_panelLayer = PanelLayer::create();
this->addChild(_panelLayer);



实现一个空的pause函数

void GameScene::pause()
{

}