CCControlButton的用法

来源:互联网 发布:finalcaption字幕软件 编辑:程序博客网 时间:2024/04/28 12:12
//【一】:创建:1.方法:(1):CCControlButton::create("CCScale9Sprite背景对象");(2):CCControlButton::create("CCLabelTTF对象","CCScale9Sprite背景对象");【二】:函数:       //当按钮响应对应事件后的CCScale9Sprite对象setBackgroundSpriteForState("新的CCScale9Sprite对象","CCControlState状态变量");       //当按钮响应对应事件后的文字颜色setTitleColorForState("新的ccColor3B颜色","CCControlState状态变量");       //当按钮响应对应事件后的文本setTitleForState("新的CCString对象","CCControlState状态变量");CCControlState状态变量如下(就是说处于这个状态才调用):CCControlStateDisabled            //禁用CCControlStateHighlighted        //高亮CCControlStateNormal               //正常CCControlStateSelected            //被XX过后 (感谢 北京|殁如雪 同学的解释)CCControlState事件如下:CCControlEvenTouchDown                   //按下CCControlEvenTouchDragInside          //在其内部拖动CCControlEvenTouchDragOutside       //在其外部拖动CCControlEvenTouchDragEnter           //拖动进入其内部CCControlEvenTouchDragExit              //拖动离开其内部CCControlEvenTouchUpinside              //在其内部抬起CCControlEvenTouchUpOutside           //在其外部抬起CCControlEvenTouchCancel                 //取消所有触点【三】:示例:首先呢,为了做出效果,我们要准备2张图片,已经给大家准备好了。[gl.paea.cn-CCControlButton-button1.png][gl.paea.cn-CCControlButton-button2.png]Controlbutton.h文件1.包含“cocos-ext.h”文件。#include "cocos-ext.h"2.引用命名空间“cocos2d::extension”。using namespace cocos2d::extension;3.设置4个回调函数,一会儿给两个按钮实现不同效果void downaction1(CCObject * sender,CCControlEvent);void upinsideaction1(CCObject * sender,CCControlEvent);void downaction2(CCObject * sender,CCControlEvent);void upinsideaction2(CCObject * sender,CCControlEvent);[gl.paea.cn-CCControlButton5.png]Controlbutton.cpp文件1.引用命名空间“cocos2d::extension”。using namespace cocos2d::extension;2.引用一下CocosDenshion命名空间 using namespace CocosDenshion;3.加载我们的2张图片。4.开始写码//--new--//CCSize mysize=CCDirector::sharedDirector()->getWinSize();//新建三个c9对象//这里要说一下为啥创建两个一样的c9对象c91,c93因为后面要用//他们分别创建2个对象,不能同指针不然会报错哦。CCScale9Sprite * c91=CCScale9Sprite::create("button1.png");CCScale9Sprite * c92=CCScale9Sprite::create("button2.png");CCScale9Sprite * c93=CCScale9Sprite::create("button1.png");//新建2个ttf对象CCLabelTTF * ttf=CCLabelTTF::create("hello","Arial",20);CCLabelTTF * statettf=CCLabelTTF::create("no object","Arial",20);//用2种方法新建两个CCControlButton对象CCControlButton * mybutton1=CCControlButton::create(c91);CCControlButton * mybutton2=CCControlButton::create(ttf,c93);//设置位置mybutton1->setPosition(ccp(mysize.width/2-100,mysize.height/2));mybutton2->setPosition(ccp(mysize.width/2+100,mysize.height/2));//设置大小mybutton1->setPreferredSize(CCSizeMake(80,30));mybutton2->setPreferredSize(CCSizeMake(80,30));//设置按下事件//这里的意思是mybutton1处于CCControlStateHighlighted高亮状态调用c92。mybutton1->setBackgroundSpriteForState(c92,CCControlStateHighlighted);mybutton2->setTitleColorForState(ccc3(120,120,120),CCControlStateHighlighted);mybutton2->setTitleForState(CCString::create("hi"),CCControlStateHighlighted);//设置按下回调事件mybutton1->addTargetWithActionForControlEvents(this,cccontrol_selector(Controlbutton::downaction1),CCControlEventTouchDown);mybutton1->addTargetWithActionForControlEvents(this,cccontrol_selector(Controlbutton::upinsideaction1),CCControlEventTouchUpInside);mybutton2->addTargetWithActionForControlEvents(this,cccontrol_selector(Controlbutton::downaction2),CCControlEventTouchDown);mybutton2->addTargetWithActionForControlEvents(this,cccontrol_selector(Controlbutton::upinsideaction2),CCControlEventTouchUpInside);//设置状态显示statettf->setPosition(ccp(mysize.width/2,mysize.height/2-100));//加载this->addChild(statettf,0,521);this->addChild(mybutton1);this->addChild(mybutton2);//--new--//[gl.paea.cn-CCControlButton4.jpg]       然后我们把4个回调函数写好void Controlbutton::downaction1(CCObject * sender,CCControlEvent){CCLabelTTF * statettf=(CCLabelTTF*)this->getChildByTag(521);statettf->setString(CCString::createWithFormat("button1 down")->getCString());}void Controlbutton::downaction2(CCObject * sender,CCControlEvent){CCLabelTTF * statettf=(CCLabelTTF*)this->getChildByTag(521);statettf->setString(CCString::createWithFormat("button2 down")->getCString());}void Controlbutton::upinsideaction1(CCObject * sender,CCControlEvent){CCLabelTTF * statettf=(CCLabelTTF*)this->getChildByTag(521);statettf->setString(CCString::createWithFormat("button1 up")->getCString());}void Controlbutton::upinsideaction2(CCObject * sender,CCControlEvent){CCLabelTTF * statettf=(CCLabelTTF*)this->getChildByTag(521);statettf->setString(CCString::createWithFormat("button2 up")->getCString());}[gl.paea.cn-CCControlButton3.png]       好了,我们运行一下看看。[gl.paea.cn-CCControlButton2.png]       OK,成功了

0 0
原创粉丝点击