天天看點

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
10 cocos2d-x 按鈕 CCControlButton
10 cocos2d-x 按鈕 CCControlButton