cocos2d-x按钮菜单(cocos2d-x2.1)

首先看看程序运行时:


程序中有四种菜单项按钮:文本菜单项(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菜单Label
		pStopLabel = 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 callback
	void 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 instance
bool 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菜单Label
		pStopLabel = 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 animation
		CCSpriteFrameCache *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,CCMenu)