Cocos2d-x_CCControlButton(按钮类)介绍

//
// HelloWorldScene.h
//

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;

class HelloWorld : public cocos2d::CCLayer
{
public:
    virtual bool init();
    static cocos2d::CCScene* scene();

    CREATE_FUNC(HelloWorld);
    
    void touchDownAction(CCObject *pSender,CCControlEvent controlEvent);
    void touchUpInsideAction(CCObject *pSender,CCControlEvent controlEvent);
    void touchUpOutsideAction(CCObject *pSender,CCControlEvent controlEvent);
};

#endif

//
// HelloWorldScene.cpp
//

#include "HelloWorldScene.h"

USING_NS_CC;

CCScene* HelloWorld::scene()
{
    CCScene *scene = CCScene::create();
    HelloWorld *layer = HelloWorld::create();
    scene->addChild(layer);

    return scene;
}

bool HelloWorld::init()
{
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCLabelTTF *ttf = CCLabelTTF::create("未选中文字","MarkerFelt",25);
    CCScale9Sprite *bgSpr = CCScale9Sprite::create("button.png");
    CCControlButton *ccBtn = CCControlButton::create(ttf,bgSpr);
    ccBtn->setPosition(ccp(240,170));
    
    ccBtn->setBackgroundSpriteForState(CCScale9Sprite::create("buttonHighlighted.png"),CCControlStateHighlighted);
    ccBtn->setTitleColorForState(ccc3(255,0),CCControlStateHighlighted);
    ccBtn->setTitleForState(CCString::create("选中文字"),CCControlStateHighlighted);
    
    ccBtn->addTargetWithActionForControlEvents(this,cccontrol_selector(HelloWorld::touchDownAction),CCControlEventTouchDown);
    ccBtn->addTargetWithActionForControlEvents(this,cccontrol_selector(HelloWorld::touchUpInsideAction),CCControlEventTouchUpInside);
    ccBtn->addTargetWithActionForControlEvents(this,cccontrol_selector(HelloWorld::touchUpOutsideAction),CCControlEventTouchUpOutside);
    this->addChild(ccBtn);

    CCLabelTTF *ttfBtnState = CCLabelTTF::create("",25);
    ttfBtnState->setPosition(ccp(240,220));
    this->addChild(ttfBtnState,293);
    
    return true;
}

void HelloWorld::touchDownAction(cocos2d::CCObject *pSender,CCControlEvent controlEvent)
{
    CCLabelTTF *ttf = (CCLabelTTF *)this->getChildByTag(293);
    ttf->setString("按下");
}

void HelloWorld::touchUpInsideAction(cocos2d::CCObject *pSender,CCControlEvent controlEvent)
{
    CCLabelTTF *ttf = (CCLabelTTF *)this->getChildByTag(293);
    ttf->setString("内部抬起");
}

void HelloWorld::touchUpOutsideAction(cocos2d::CCObject *pSender,CCControlEvent controlEvent)
{
    CCLabelTTF *ttf = (CCLabelTTF *)this->getChildByTag(293);
    ttf->setString("外部抬起");
}


相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...