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)
{

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值