cocos 2 d-xボタンメニュー(cocos 2 d-x 2.1)

7908 ワード

まず、プログラムの実行時を見てみましょう.
プログラムには4つのメニュー項目ボタンがあります.テキストメニュー項目(stop walk)、フォントメニュー項目(Hide Bear)、Toggleメニュー項目(Go Right)、ピクチャメニュー(プログラムスイッチ)です.
直感的な表示のため、本プログラムは前のブログ(アニメーション制作)に基づいて修正します.
cococos 2 d-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;
	}
}