CCControlButton 按鈕

CCControlButton按鈕也是cocos2d-x擴展的一個組件,還是應用程序中最常見、使用效率最高的組件

HelloWorld.h文件

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "cocos-ext.h"
#include "SimpleAudioEngine.h"
using namespace cocos2d::extension;

class HelloWorld : public cocos2d::CCLayer,public cocos2d::extension::CCEditBoxDelegate
{
public:
    // 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();
    
    CREATE_FUNC(HelloWorld);

	//按鈕按下回調函數
	void touchDownAction(CCObject* pSender,CCControlEvent controlEvent);
	//按鈕在內部擡起回調函數
	void touchUpInsedeAction(CCObject* pSender,CCControlEvent controlEvent);
	//按鈕在外部擡起回調函數
	void touchUpOutsideAction(CCObject* pSender,CCControlEvent controlEvent);
};

#endif  // __HELLOWORLD_SCENE_H__


HelloWorld.cpp文件

#include "HelloWorldScene.h"
#include "cocos-ext.h"
#include "cocos2d.h"

using namespace cocos2d;
using namespace cocos2d::extension;

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 
    {
        //////////////////////////////////////////////////////////////////////////
        // super init first
        //////////////////////////////////////////////////////////////////////////

        CC_BREAK_IF(! CCLayer::init());
		CCSize size = CCDirector::sharedDirector()->getWinSize();
		
		
		//按鈕
		CCLabelTTF* pBtnLabel = CCLabelTTF::create("no","MarkerFelt",25);
		CCControlButton* pBtn = CCControlButton::create(
			pBtnLabel,
			CCScale9Sprite::create("button.png")
			);
		CCControlButton* pBtn = CCControlButton::create(
			CCScale9Sprite::create("button.png")
			);
		pBtn->setPosition(ccp(50,50));

		//按鈕被選中後背景圖響應的狀態
		pBtn->setBackgroundSprite(CCScale9Sprite::create("buttonHighlighted.png"));
		//按鈕被選中後文字顏色響應的狀態
		pBtn->setTitleColorForState(ccc3(255,0,0),CCControlStateHighlighted);
		//按鈕被選中後文字狀態響應的狀態
		pBtn->setTitleForState(
			CCString::create("yes")
			,CCControlStateHighlighted
			);
		//按鈕按下事件回調
		pBtn->addTargetWithActionForControlEvents(
			this,
			cccontrol_selector(HelloWorld::touchDownAction),
			CCControlEventTouchDown
			);
		//按鈕再其內部擡起事件回調
		pBtn->addTargetWithActionForControlEvents(
			this,
			cccontrol_selector(HelloWorld::touchUpInsedeAction),
			CCControlEventTouchUpInside
			);
		//按鈕再其外部擡起事件回調
		pBtn->addTargetWithActionForControlEvents(
			this,
			cccontrol_selector(HelloWorld::touchUpOutsideAction),
			CCControlEventTouchUpOutside
			);
setPreferredSize()//設置爲圖片大小
		this->addChild(pBtn);

        bRet = true;
    } while (0);

    return bRet;
}

//按鈕按下回調函數
void HelloWorld::touchDownAction(CCObject* pSender,CCControlEvent controlEvent)
{

}
//按鈕在內部擡起回調函數
void HelloWorld::touchUpInsedeAction(CCObject* pSender,CCControlEvent controlEvent)
{

}
//按鈕在外部擡起回調函數
void HelloWorld::touchUpOutsideAction(CCObject* pSender,CCControlEvent controlEvent)
{

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章