Cocos2D-x游戏开发之二:菜单事件响应
来源:互联网 发布:APP登录HTML源码 编辑:程序博客网 时间:2024/04/30 01:24
接着描述Cocos2D-x基础知识。这一节我描述一下Cocos2D-x菜单的定义和响应。
先上代码吧,我们直接在helloworld的init函数中做更改调试:
bool HelloWorld::init()
{
bool bRet = false;
do
{
//////////////////////////////////////////////////////////////////////////
// super init first
//////////////////////////////////////////////////////////////////////////
CC_BREAK_IF(! CCLayer::init());
//////////////////////////////////////////////////////////////////////////
CCSize size = CCDirector::sharedDirector()->getWinSize();
CCMenuItemImage *pCloseItem = CCMenuItemImage::create("CloseNormal.png","CloseSelected.png",this,menu_selector(HelloWorld::menuCloseCallback));
CC_BREAK_IF(! pCloseItem);
// Place the menu item bottom-right conner.
pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));
CCLabelTTF *label = CCLabelTTF::create("Show Sprite","Arial",30);
CCMenuItemLabel *plabel = CCMenuItemLabel::create(label,this,menu_selector(HelloWorld::LabelCallback));
// Create a menu with the "close" menu item, it's an auto release object.
CCMenu* pMenu = CCMenu::create(pCloseItem,plabel, NULL);
pMenu->setPosition(size.width/2,size.height/2);
CC_BREAK_IF(! pMenu);
pMenu->alignItemsVertically();
// Add the menu to HelloWorld layer as a child layer.
this->addChild(pMenu, 2);
CCSprite* pSprite = CCSprite::create("HelloWorld.png");
CC_BREAK_IF(! pSprite);
pSprite->setPosition(ccp(size.width/2, size.height/2));
pSprite->setVisible(false);
this->addChild(pSprite, 0,1);
bRet = true;
} while (0);
return bRet;
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
// "close" menu item clicked
CCDirector::sharedDirector()->end();
}
void HelloWorld::LabelCallback(CCObject* pSender)
{
CCSprite *spr=(CCSprite*)this->getChildByTag(1);
if (spr->isVisible())
{
spr->setVisible(false);
}
else
spr->setVisible(true);
}
下面对代码加以解释:
首先菜单子项:CCMenuItem 有很多种形式的实现我们这里用了最常用的MenuItemImag 和MenuItemLabel 其具体的描述请参见文档。
CmenuItem需要加入到容器CCMenu中来使用(其实这里的CCMenu好比CCsene我只这么理解的)然后将CCMenu加入到Layer中就能在场景中显示了。
注意设置菜单事件用menu_selector()来实现。
demo源码:http://download.csdn.net/detail/vanquishedzxl/7047123
- Cocos2D-x游戏开发之二:菜单事件响应
- cocos2d-x游戏开发之二
- Cocos2d-x游戏开发之2.x后弹出键盘后无法响应除键盘外的触摸事件解决
- Cocos2d-X游戏开发(二)
- 【Cocos2d-x游戏引擎开发笔记(3)】在屏幕上渲染菜单并添加消息响应
- 【Cocos2d-x游戏引擎开发笔记(3)】在屏幕上渲染菜单并添加消息响应
- 【Cocos2d-x游戏引擎开发笔记(3)】在屏幕上渲染菜单并添加消息响应
- [ IOS-Cocos2d-x 游戏开发] - cocosBuilder 开发之二
- Cocos2d-x游戏开发之处理单点触摸事件
- Cocos2d-x游戏开发之触屏事件详解
- 【iOS-cocos2d-X 游戏开发之二】【必看篇】总结阐述Cocos2d-X与Cocos2d-iphone区别;
- 【iOS-cocos2d-X 游戏开发之二】【必看篇】总结阐述Cocos2d-X与Cocos2d-iphone区别;
- 【iOS-cocos2d-X 游戏开发之二】【必看篇】总结阐述Cocos2d-X与Cocos2d-iphone区别
- 【iOS-cocos2d-X 游戏开发之二】【必看篇】总结阐述Cocos2d-X与Cocos2d-iphone区别
- cocos2d-x 游戏开发系列之二:新建win32项目
- 游戏开发教程cocos2d-x移植之二
- vs2010 cocos2d-x 游戏开发之旅 二
- 《cocos2d-x游戏开发之旅》学习笔记(二)
- Provider-Defined Properties in DBPROPSET_JETOLEDB_DBINIT
- PPT单一对象设置多个动作、多个对象设置同一动作
- 第七章代码2
- J2ME的小飞机游戏
- 2440 Linux按键驱动及测试程序
- Cocos2D-x游戏开发之二:菜单事件响应
- Python字符编码详解
- 2012华为校园招聘机考试题总结(厦门大学) 渣渣的答案= =!
- 最全的IO操作知识总结
- SG函数模板
- 计科1111-1114班第二周讲义、课外作业(截止日期:2014年3月20日23点-周四晚,学委飞信通知同学)
- javaBean
- static
- inux按键中断驱动程序——S3C2440