使用返回键和菜单键需要复写两个方法
1、在头文件中复写
virtual void keyBackClicked();//Android 返回键
virtual void keyMenuClicked();//Android 菜单键
2、实现
void HelloWorld::keyBackClicked()
{
PopupLayer* popLayer = PopupLayer::create();
this->addChild(popLayer);
}
void HelloWorld::keyMenuClicked()
{
//PopupLayer* popLayer = PopupLayer::create();
//this->addChild(popLayer);
}
3、同事在init中设置
this->setKeypadEnabled(true);
自定义菜单项
头文件
#include "cocos2d.h"
USING_NS_CC;
class PopupLayer: public CCLayer
{
public:
static CCScene* scene();
bool init();
CREATE_FUNC(PopupLayer);
private:
//注册触摸事件,实现ccTouchBegan()方法
void registerWithTouchDispatcher();
bool ccTouchBegan(CCTouch* touch,CCEvent* pevent);
//在弹出的对话框上加俩个按钮,以下的函数是对应的按钮的处理事件
void yesButton(CCObject* object);
void noButton(CCObject* object);
//设置对话框的title
void setTitle();
//设置对话框的文本内容
void setContent();
//m_size代表的是对话框背景的大小
CCSize m_size;
//对话框的背景精灵
CCSprite* m_bgSprite;
};
实现
#include "PopupLayer.h"
#include "cocos2d.h"
USING_NS_CC;
CCScene* PopupLayer::scene()
{
CCScene* scene = NULL;
do
{
scene = CCScene::create();
PopupLayer* layer = PopupLayer::create();
scene->addChild(layer);
}
while(0);
return scene;
}
bool PopupLayer::init()
{
bool bRet = false;
do
{
CC_BREAK_IF(!CCLayer::init());
CCSize winSize =
CCDirector::sharedDirector()->getWinSize();
//设置这个层的背景图片,并且设置其位置为整个屏幕的中点
CCSprite* background =
CCSprite::create("backbg.png");
m_bgSprite = background;
background->setPosition(ccp(winSize.width/2,
winSize.height/2));
this->addChild(background);
//获得背景图片的大小
CCSize contentSize = background->getContentSize();
m_size = contentSize;
//添加俩个菜单在这个层中
CCMenuItemImage* item1 =
CCMenuItemImage::create("gfx/exitbtn.png",
"gfx/exitbtn.png","",
this,menu_selector(PopupLayer::yesButton));
CCMenuItemImage* item2 =
CCMenuItemImage::create("gfx/backbtn.png",
"gfx/backbtn.png","",
this,menu_selector(PopupLayer::noButton));
CCMenu* menu = CCMenu::create(item1,item2,NULL);
menu->alignItemsHorizontallyWithPadding(15);
menu->setPosition(ccp(contentSize.width/2,
contentSize.height/3));
//kCCMenuHandlerPriority的值为-128,代表的是菜单按钮的触摸优先级
//设置menu优先级,这里设置为普通menu的二倍减一,原因看下边
menu->setTouchPriority(kCCMenuHandlerPriority*2
-1);
background->addChild(menu);
//设置题目和文本内容
this->setTitle();
this->setContent();
this->setTouchEnabled(true);
bRet = true;
}
while(0);
CCDirector::sharedDirector()->pause();
return bRet;
}
void PopupLayer::registerWithTouchDispatcher()
{
//kCCMenuHandlerPriority=-128,将这个值设置为-128的二倍,可以比下边的层的优先级高
//而且ccTouchBegan的返回值为true,说明其他的层将接受不到这个触摸消息了,只有这个层上边的
//菜单的优先级比他还要打,所以它上边的菜单是可以接收到触摸消息的
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(
this,
kCCMenuHandlerPriority*2,true);
}
bool PopupLayer::ccTouchBegan(CCTouch* touch,
CCEvent* pevent)
{
return true;
}
//点击菜单按钮的时候调用的事件处理函数
void PopupLayer::yesButton(CCObject* object)
{
CCDirector::sharedDirector()->end();
this->removeFromParentAndCleanup(true);
}
void PopupLayer::noButton(CCObject* object)
{
CCDirector::sharedDirector()->resume();
this->removeFromParentAndCleanup(true);
}
//设置这个层的题目
void PopupLayer::setTitle()
{
CCLabelTTF* title = CCLabelTTF::create("Exit?",
"",24);
//CCLabelBMFont* title =
// CCLabelBMFont::create("ok",
//"default.fnt");
title->setPosition(ccp(m_size.width/2,
m_size.height-title->getContentSize().height/2
-10));
m_bgSprite->addChild(title);
}
//设置层的内容
void PopupLayer::setContent()
{
CCLabelTTF* content =
CCLabelTTF::create("sure to exit?",
"",24);
content->setPosition(ccp(m_size.width/2,
m_size.height/2));
//设置ttf的文本域
content->setDimensions(CCSize(this->m_size.width
-60,this->m_size.height-80));
//设置ttf的水平对齐方式
content->setHorizontalAlignment(
kCCTextAlignmentLeft);
m_bgSprite->addChild(content);
}