您的位置:首页 >> Cocos2d-x >> 和屌丝一起学cocos2dx-CCControlButton

和屌丝一起学cocos2dx-CCControlButton

[ 孤狼 | 2015-02-09 17:13:16 | Cocos2d-x | 2603°C | 0条评论 ]
声 明

本教程仅用于初学cocos2dx同学使用,内容由本人(孤狼)学习过程中笔记编写,本教程使用cocos2dx版本为2.1.4。本教程内容可以自由转载,但必须同时附带本声明,或注明出处。
gl.paea.cn版权所有。


       大家好,欢迎回到“和屌丝一起学cocos2dx”系列教程。今天是第十一节了,我们还要继续加油学下去哦。上节我们说到了“CCControlSlider”相信大家都已经做出来了吧。是不是很有成就感呢?嘿嘿。好了我们今天还要说一个比较常见的控件-“CCControlButton”。一起来学习吧。


       我们新建一个项目名字叫Controlbutton。


       这里我要说一下,有不少同学是直接把我的代码里的.h和.cpp文件拷贝进他的项目,结果运行报错,这里要和大家说一个下,每个类的最上面

#ifndef __HELLOWORLD_SCENE_H__

#define __HELLOWORLD_SCENE_H__



       这里是定义了一个常量。目的就是为了区分每个不同的类。如果发现常量已经被定义过就跳出,我的代码中是没有helloworld.h这个文件的,如果你直接把我的代码加入项目,记得修改这里。或者和我一样删除自带的helloworld项目。


【一】:创建:


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张图片,已经给大家准备好了。




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);





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--//



       然后我们把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());

}



       好了,我们运行一下看看。

       OK,成功了


本节DEMO下载


转载请注明出处:http://gl.paea.cn/n.php?n=15
 
如您看得高兴,欢迎随意投喂,让我们坚持创作!
赞赏一个鸡腿
还没有人留下遗迹
综合 · 搜索