cocos2d-x 返回键 和菜单的使用

本文介绍了在cocos2d-x游戏中如何处理Android的返回键和菜单键。通过复写`keyBackClicked()`和`keyMenuClicked()`方法,并创建PopupLayer类来显示对话框,当用户点击返回键或菜单键时,会弹出确认退出的对话框。此外,还展示了如何设置对话框的标题和内容,并响应用户的选择。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用返回键和菜单键需要复写两个方法

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);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值