加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Cocos2d-x_CCControlButton(按钮类)介绍

发布时间:2020-12-14 19:09:09 所属栏目:百科 来源:网络整理
导读://// 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 cocos2
//
// 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("外部抬起");
}


(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读