cocos2d-x按钮菜单(cocos2d-x2.1)
来源:互联网 发布:软件工作量估计 编辑:程序博客网 时间:2024/04/29 05:26
首先看看程序运行时:
程序中有四种菜单项按钮:文本菜单项(stop walk),图字菜单项(Hide Bear),Toggle菜单项(Go Right)以及图片菜单(程序开关)。
为了有直观的显示,本程序在上一篇博客(动画制作)基础上修改。
cocos2d-x的菜单CCMenu的创建有几个步骤
1. 创建菜单项(CCMenuItem),并设置位置、点击回调函数。CCMenuItemImage,CCMenuItemLabel,CCMenuItemToggle
2. 将菜单项添加到菜单创建的参数中,并为菜单设置位置、添加到层。
例如:
////////////////////////////////////////////////////////////////////////////// 菜单创建 // 图片菜单按钮项 CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); CC_BREAK_IF(! pCloseItem); pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));//Pause Resume菜单LabelpStopLabel = CCLabelTTF::create("Stop Walk!", "Arial", 20);pShowLabel = CCLabelBMFont::create("Hide Bear", "Arial.fnt");CCMenuItemLabel* pItemLabel = CCMenuItemLabel::create(pStopLabel, this, menu_selector(HelloWorld::menuStopCallback));CCMenuItemLabel* pItemLabel2 = CCMenuItemLabel::create(pShowLabel, this, menu_selector(HelloWorld::menuShowCallback));pItemLabel->setPosition(ccp(winSize.width * 0.5 - 60, winSize.height * 0.8));pItemLabel2->setPosition(ccp(winSize.width * 0.5 + 60, winSize.height * 0.8));CCMenuItemFont::setFontName("Arial");CCMenuItemFont::setFontSize(20);CCMenuItemToggle* pItemToggle = CCMenuItemToggle::createWithTarget(this, menu_selector(HelloWorld::menuTogglItemCallback), CCMenuItemFont::create("Go Right"), CCMenuItemFont::create("Go Left"), NULL);pItemToggle->setPosition(ccp(winSize.width * 0.5, winSize.height * 0.2));CCMenu *pMenu = CCMenu::create(pCloseItem, pItemLabel, pItemLabel2, pItemToggle, NULL);pMenu->setPosition(CCPointZero);CC_BREAK_IF(!pMenu);this->addChild(pMenu);
本程序的HelloWorld代码:
#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"#include "SimpleAudioEngine.h"class HelloWorld : public cocos2d::CCLayer{public:HelloWorld();virtual ~HelloWorld(); // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init(); // there's no 'id' in cpp, so we recommand to return the exactly class pointer static cocos2d::CCScene* scene(); // a selector callback void menuCloseCallback(CCObject* pSender); // implement the "static node()" method manually CREATE_FUNC(HelloWorld);cocos2d::CCSpriteBatchNode *_actors;cocos2d::CCSprite *_bear;// menu callbackvoid menuStopCallback(CCObject* sender);void menuShowCallback(CCObject* sender);void menuTogglItemCallback(CCObject* sender);protected:bool bLabelBtnStop;bool bSpriteIsShow;bool bGoLeft;cocos2d::CCLabelTTF* pStopLabel;cocos2d::CCLabelBMFont* pShowLabel;};#endif // __HELLOWORLD_SCENE_H__
#include "HelloWorldScene.h"using namespace cocos2d;HelloWorld::HelloWorld(){_actors = NULL;_bear = NULL;bLabelBtnStop = true;bSpriteIsShow = true;pStopLabel = NULL;pShowLabel = NULL;bGoLeft = true;}HelloWorld::~HelloWorld(){}CCScene* HelloWorld::scene(){ CCScene * scene = NULL; do { // 'scene' is an autorelease object scene = CCScene::create(); CC_BREAK_IF(! scene); // 'layer' is an autorelease object HelloWorld *layer = HelloWorld::create(); CC_BREAK_IF(! layer); // add layer as a child to scene scene->addChild(layer); } while (0); // return the scene return scene;}// on "init" you need to initialize your instancebool HelloWorld::init(){ bool bRet = false; do { CC_BREAK_IF(! CCLayer::init());CCSize winSize = CCDirector::sharedDirector()->getWinSize();////////////////////////////////////////////////////////////////////////////// 菜单 // 图片菜单按钮项 CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); CC_BREAK_IF(! pCloseItem); pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));//Pause Resume菜单LabelpStopLabel = CCLabelTTF::create("Stop Walk!", "Arial", 20);pShowLabel = CCLabelBMFont::create("Hide Bear", "Arial.fnt");CCMenuItemLabel* pItemLabel = CCMenuItemLabel::create(pStopLabel, this, menu_selector(HelloWorld::menuStopCallback));CCMenuItemLabel* pItemLabel2 = CCMenuItemLabel::create(pShowLabel, this, menu_selector(HelloWorld::menuShowCallback));pItemLabel->setPosition(ccp(winSize.width * 0.5 - 60, winSize.height * 0.8));pItemLabel2->setPosition(ccp(winSize.width * 0.5 + 60, winSize.height * 0.8));CCMenuItemFont::setFontName("Arial");CCMenuItemFont::setFontSize(20);CCMenuItemToggle* pItemToggle = CCMenuItemToggle::createWithTarget(this, menu_selector(HelloWorld::menuTogglItemCallback), CCMenuItemFont::create("Go Right"), CCMenuItemFont::create("Go Left"), NULL);pItemToggle->setPosition(ccp(winSize.width * 0.5, winSize.height * 0.2));CCMenu *pMenu = CCMenu::create(pCloseItem, pItemLabel, pItemLabel2, pItemToggle, NULL);pMenu->setPosition(CCPointZero);CC_BREAK_IF(!pMenu);this->addChild(pMenu);////////////////////////////////////////////////////////////////////////////////////////////////////////// add bear animationCCSpriteFrameCache *cache = CCSpriteFrameCache::sharedSpriteFrameCache();cache->addSpriteFramesWithFile("Bears.plist");_actors = CCSpriteBatchNode::create("Bears.png");this->addChild(_actors);_bear = CCSprite::createWithSpriteFrameName("bear1.png");_bear->setPosition(ccp(winSize.width / 2, winSize.height / 2 ));_actors->addChild(_bear);// 为每帧创建图片CCArray *walkFrames = CCArray::createWithCapacity(8);for (int i = 1; i <= 8; i++){CCSpriteFrame *frame = cache->spriteFrameByName(CCString::createWithFormat("bear%1d.png", i)->getCString());walkFrames->addObject(frame);}// 创建动画CCAnimation *walkAnimation = CCAnimation::createWithSpriteFrames(walkFrames, 1.0f / 12.0f);CC_BREAK_IF(!walkAnimation);CCAnimate* walkAnimate = CCAnimate::create(walkAnimation);CC_BREAK_IF(!walkAnimate);// 运行动画动作_bear->runAction(CCRepeatForever::create(walkAnimate)); bRet = true; } while (0); return bRet;}void HelloWorld::menuCloseCallback(CCObject* pSender){ // "close" menu item clicked CCDirector::sharedDirector()->end();}void HelloWorld::menuStopCallback(CCObject* pSender){if (bLabelBtnStop){_bear->pauseSchedulerAndActions();bLabelBtnStop = false;pStopLabel->setString("Go Walk");}else{_bear->resumeSchedulerAndActions();bLabelBtnStop = true;pStopLabel->setString("Stop Walk");}}void HelloWorld::menuShowCallback(CCObject* sender){if (bSpriteIsShow){_bear->setVisible(false);pShowLabel->setString("Show Bear");bSpriteIsShow = false;}else{_bear->setVisible(true);pShowLabel->setString("Hide Bear");bSpriteIsShow = true;}}void HelloWorld::menuTogglItemCallback(CCObject* sender){if (bGoLeft){_bear->setScaleX(-1);bGoLeft = false;}else{_bear->setScaleX(1);bGoLeft = true;}}
- cocos2d-x按钮菜单(cocos2d-x2.1)
- cocos2d-x动画制作(cocos2d-x2.1)
- cocos2d-x GUI控件的使用(cocos2d-x2.1)
- box2d弹球 cocos2d-x重力感应(cocos2d-x2.1)
- box2d弹球 cocos2d-x重力感应(cocos2d-x2.1)
- Cocos2d-x的学习之旅(五)菜单按钮
- cocos2d-x 单选按钮菜单
- Cocos2D-X学习7:菜单按钮
- Box2D Breakout游戏(cocos2d-x2.1)
- (coco2d-x初学)xcode5.0安装 cocos2d-x2.2.0
- cocos2d-x 按钮
- cocos2d-x按钮设置
- cocos2d-x菜单
- cocos2d-x 菜单项
- cocos2d-x菜单项
- cocos2d-x CCMenu 菜单
- cocos2d-x:菜单项
- Cocos2d-x文本菜单
- openssl的使用
- java try、catch、finally及finally执行顺序详解
- make 出现未定义文件中的符号在文件中
- javascript正则表达式网址 标题的显示
- 事件(event)
- cocos2d-x按钮菜单(cocos2d-x2.1)
- SY-SUBRC 的含义
- contentProvider中有关query方法的使用
- 微软向Win 7用户推出IE10 争抢Chrome浏览器份额
- &和&&的区别
- 在ubuntu下实现远程登录桌面
- tr命令
- Common FileUpload组件的使用!
- 离职 需要注意三个问题