现在的位置: 首页 > 综合 > 正文

Cocos2d-x_CCControlButton(按钮类)介绍

2016年08月30日 ⁄ 综合 ⁄ 共 2244字 ⁄ 字号 评论关闭
//
// 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, 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("", "MarkerFelt", 25);
    ttfBtnState->setPosition(ccp(240, 220));
    this->addChild(ttfBtnState,0,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("外部抬起");
}


抱歉!评论已关闭.