10 cocos2d-x 按钮 CCControlButton

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "cocos-ext.h"
using namespace cocos2d::extension;
class HelloWorld : public cocos2d::CCLayer
{
public:
    // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
    virtual bool init();

    // there's no 'id' in cpp, so we recommend to return the class instance pointer
    static cocos2d::CCScene* scene();
    
    // a selector callback
    void menuCloseCallback(CCObject* pSender);
    
    //按下事件的回调
    void touchDownAction(CCObject *sender,CCControlEvent * event );
    //按钮在七内部抬起事件的回调
    void touchUpInsideAction(CCObject *sender,CCControlEvent * event );
    //按钮在七外部抬起的事件的回调
    void touchUpOutSideAction(CCObject *sender,CCControlEvent * event);
    

    // preprocessor macro for "static create()" constructor ( node() deprecated )
    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__
 
 
 
 
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
#include "cocos-ext.h"
#include "CCControlButton.h"
using namespace cocos2d;
using namespace CocosDenshion;
using namespace cocos2d::extension;

CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }

    //CCControlButton 按钮
    CCLabelTTF *label=CCLabelTTF::create("按钮", "MarkerFelt", 20);
    CCScale9Sprite *sprite=CCScale9Sprite::create("Icon.png");
    CCControlButton *btn=CCControlButton::create(label, sprite);
    btn->setPosition(ccp(240, 170));
    
    
    //按钮被选中背景图片响应的状态
    btn->setBackgroundSpriteForState(CCScale9Sprite::create("123.png"), CCControlStateHighlighted);
    //按钮被选中后文字颜色响应的状态
    btn->setTitleColorForState(ccc3(0, 0, 255), CCControlStateHighlighted);
    //按钮被选中后文字响应的状态
    btn->setTitleForState(CCString::create("选中"), CCControlStateHighlighted);
    this->addChild(btn);
    
    //按钮按下事件回调
    btn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchDownAction), CCControlEventTouchDown);
    //按钮在七内部抬起事件的回调

    btn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchUpInsideAction), CCControlEventTouchUpInside);
    //按钮在七外部抬起的事件的回调
    btn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchUpOutSideAction), CCControlEventTouchUpOutside);
    
    //用于显示按钮的状态
    CCLabelTTF *label2=CCLabelTTF::create("", "MarkerFelt", 20);
    label2->setPosition(ccp(240, 220));
    this->addChild(label2,0,90);

    return true;
}

//按下事件的回调
void HelloWorld::touchDownAction(CCObject *sender,CCControlEvent * event )
{
    CCLabelTTF *label=(CCLabelTTF *)this->getChildByTag(90);
    label->setString(CCString::createWithFormat("按下")->getCString());
}
//按钮在七内部抬起事件的回调
void HelloWorld::touchUpInsideAction(CCObject *sender,CCControlEvent * event )
{
    CCLabelTTF *label=(CCLabelTTF *)this->getChildByTag(90);
    label->setString(CCString::createWithFormat("内部抬起")->getCString());
}
    //按钮在七外部抬起的事件的回调
void HelloWorld::touchUpOutSideAction(CCObject *sender,CCControlEvent * event)
{
    CCLabelTTF *label=(CCLabelTTF *)this->getChildByTag(90);
    label->setString(CCString::createWithFormat("外部抬起")->getCString());
}



10 cocos2d-x 按钮 CCControlButton_第1张图片

10 cocos2d-x 按钮 CCControlButton_第2张图片

10 cocos2d-x 按钮 CCControlButton_第3张图片


   

你可能感兴趣的:(10 cocos2d-x 按钮 CCControlButton)