// WCBaseLayer.h
// 原理:
// 模态对话框 能够拦截所有下层的消息
// cocos2d-x 消息处理优先级【-128,128】越小优先级越高
// 菜单kCCMenuHandlerPriority优先级 -128要想拦截所有消息(包括菜单)
// 必须将我们的模态对话框优先级<-128 但是也意味着拦截了当前模态对话框的菜单消息(-128)
// 我们需要手动将消息传给模态对话框上的按钮进行处理
// 使用:1.重载OnInitDialog将需要的控件添加到dlg上
// 2.需要响应事件的按钮使用pushMenu添加
// 3.派生类需要实现宏CREATE_FUNC(class_name)
#ifndef _WCBASELAYER_H_
#define _WCBASELAYER_H_
#include "cocos2d.h"
class WCBaseLayer: public cocos2d::CCLayerColor
{
public:
WCBaseLayer();
virtual ~WCBaseLayer();
public:
/** 初始化对话框 */
virtual bool initWithMenu(cocos2d::CCMenu *pMenu);
virtual void onEnter();
virtual void onExit();
virtual bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
virtual void ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
virtual void ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
virtual void ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
private:
cocos2d::CCMenu *m_menu; // 模态对话框菜单
bool m_touched; // 记录菜单点击
};
#endif
//WCBaseLayer.cpp
#include "WcBaseLayer.h"
USING_NS_CC;
WCBaseLayer::WCBaseLayer():
m_menu(NULL),
m_touched(false)
{
}
WCBaseLayer::~WCBaseLayer()
{
}
bool WCBaseLayer::initWithMenu(cocos2d::CCMenu *pMenu)
{
if(!CCLayerColor::initWithColor(ccc4(0, 0, 0, 125)))
{
return false;
}
m_menu = pMenu;
return true;
}
void WCBaseLayer::onEnter()
{
CCLayerColor::onEnter();
// 屏蔽所有priority比自己大的消息
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, kCCMenuHandlerPriority - 1, true);
}
void WCBaseLayer::onExit()
{
CCLayerColor::onExit();
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
}
bool WCBaseLayer::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
// 因为拦截了所有消息(包括按钮) 所以需要将消息手动传给模态对话框上的按钮
if (m_menu) {
m_touched = m_menu->ccTouchBegan(pTouch, pEvent);
}
return true;
}
void WCBaseLayer::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
if (m_touched) {
if (m_menu) {
m_menu->ccTouchMoved(pTouch, pEvent);
}
}
}
void WCBaseLayer::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
if (m_touched) {
if (m_menu) {
m_menu->ccTouchEnded(pTouch, pEvent);
}
m_touched = false;
}
}
void WCBaseLayer::ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
if (m_touched) {
if (m_menu) {
m_menu->ccTouchEnded(pTouch, pEvent);
}
m_touched = false;
}
}
//TestDlg.h
#ifndef _EXITDLG_H_
#define _EXITDLG_H_
#include "WCBaseLayer.h"
class TestDlg : public WCBaseLayer
{
public:
virtual bool init();
CREATE_FUNC(TestDlg);
//菜单回调函数
void call_ok(CCObject *pSender);
void call_cancel(CCObject *pSender);
cocos2d::CCMenu *initMenu();
public:
/** 确认和退出按钮 */
void okMenuItemCallback(CCObject *obj);
void cancelMenuItemCallback(CCObject *obj);
};
#endif
//TestDlg.cpp
#include "TestDlg.h"
#include "cocos2d.h"
USING_NS_CC;
#define NAME_OK "sure.png"
#define NAME_CANCEL "cancel.png"
#define NAME_BG "chkmsg.png"
bool TestDlg::init()
{
if (!WCBaseLayer::initWithMenu(initMenu())) {
return false;
}
return true;
}
cocos2d::CCMenu *TestDlg::initMenu()
{
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
//创建menu背景
CCSprite *sprite = CCSprite::create(NAME_BG);
sprite->setPosition(ccp(winSize.width / 2.0, winSize.height / 2.0));
this->addChild(sprite);
//创建menu
CCMenu *menu = CCMenu::create();
menu->setPosition(ccp(0, 0));
sprite->addChild(menu);
CCSize sz = sprite->getContentSize();
//创建确定、取消按钮
CCMenuItem *okMenuItem = CCMenuItemImage::create(NAME_OK, NAME_OK);
okMenuItem->setPosition(ccp(sz.width / 2.0 - okMenuItem->getContentSize().width / 1.5, okMenuItem->getContentSize().height));
okMenuItem->setTarget(this, menu_selector(TestDlg::call_ok));
menu->addChild(okMenuItem);
CCMenuItem *cancelMenuItem = CCMenuItemImage::create(NAME_CANCEL, NAME_CANCEL);
cancelMenuItem->setPosition(ccp(sz.width / 2.0 + okMenuItem->getContentSize().width / 1.5, okMenuItem->getContentSize().height));
cancelMenuItem->setTarget(this, menu_selector(TestDlg::call_cancel));
menu->addChild(cancelMenuItem);
//创建label
CCLabelTTF *lbl = CCLabelTTF::create("确定要购买?", "Arial", 45.0);
lbl->setColor(ccc3(255, 0, 0));
lbl->setPosition(ccp(sz.width / 2.0, sz.height / 2.0 + 45));
sprite->addChild(lbl);
return menu;
}
//菜单回调函数
void TestDlg::call_ok(CCObject *pSender)
{
CCLog("ok");
this->removeFromParent();
}
void TestDlg::call_cancel(CCObject *pSender)
{
CCLog("cancel");
}
void TestDlg::okMenuItemCallback(CCObject *obj)
{
CCDirector::sharedDirector()->end();
}
void TestDlg::cancelMenuItemCallback(CCObject *obj)
{
removeFromParentAndCleanup(true);
}
资源
chkmsg.png
sure.png
cancel.png