您的位置:首页 > 移动开发 > Cocos引擎

【Cocos2dx】触摸事件

2015-09-02 09:18 441 查看
Cocos2dx与玩家之间的互动,很大一个情况是通过处理玩家的触摸事件来实现的。在移动应用就是触摸,在PC应用就是点击。

下面用一个小例子说明,Cocos2dx的触摸事件。

如下图,每当用户触摸(点击)屏幕,就会生成一个触摸精灵,结束点击之后结束生成,在精灵没有结束生成之前,触摸拖动能够改变此按触摸按钮的位置。同时,任何一个触摸,都会记录当前的坐标。



***过程还是老样子,首先利用(cocos2d-x-2.2.6安装目录)\tools\project-creator下的create_project.py,新建一个用cpp语言写的名为moveAction的Cocos2dx工程,打开里面proj.win32的HelloCpp.sln开始程序编写,在AppDelegate.cpp关掉调试信息,此步骤已经在此前文章多次提及,不会的可以参看《【Cocos2dx】Windows平台下Cocos2dx 2.x的下载、安装、配置,打造自己的Helloworld》(点击打开链接)。

1、之后,现改写HelloWorldScene.h,删除原来关闭按钮的回调函数,声明四个触摸事件的函数,如下所示:

#include "cocos2d.h"

class HelloWorld : public cocos2d::CCLayer
{
public:
	// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
	virtual bool init();  

	// there's no 'id' in cpp, so we recommend returning the class instance pointer
	static cocos2d::CCScene* scene();

	// implement the "static node()" method manually
	CREATE_FUNC(HelloWorld);

	//触摸事件的函数声明
	void ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);//开始触摸
	void ccTouchesMoved(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);//触摸并移动
	void ccTouchesEnded(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);//结束触摸
	void ccTouchesCancelled(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);//触摸被强行打断

};


虽然最后一个函数void ccTouchesCancelled(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);在此例子中没有展示与实现,但这也是触摸事件的一个重要函数。用于,触摸被强行打断的情况。例如,玩家在进行Cocos2dx的游戏并正在触摸,突然来了个电话,此apps被打断,此时void ccTouchesCancelled(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);函数就生效了,一些释放、回收资源的语句可以写了此函数里面。

2、最后,改写HelloWorldScene.cpp如下:

#include "HelloWorldScene.h"

USING_NS_CC;

#define LABEL_TARGET 65535//设置文字的LABEL
int sprite_num=0;//用于判断添加按钮的个数

CCScene* HelloWorld::scene()
{
	// 'scene' is an autorelease object
	CCScene *scene = CCScene::create();

	// 'layer' is an autorelease object
	HelloWorld *layer = HelloWorld::create();

	// add layer as a child to scene
	scene->addChild(layer);

	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{

	//获取屏幕的尺寸、位置信息等    
	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();  

	//声明这个场景是存在触摸事件的
	this->setTouchEnabled(true);

	//声明文字部分  
	CCLabelTTF *label = CCLabelTTF::create("","arial",36);//声明一个没有内容文字
	label->setPosition(ccp(visibleSize.width/2,visibleSize.height-visibleSize.height/2));//按钮的中心点位于屏幕的中央    
	this->addChild(label,0,LABEL_TARGET);//添加此文字到场景中,与普通的this->addChild(label)不同,第3个参数可以理解为此文件的Tag,也就是类似其他编程语言中id的东西,其余函数可以通过this->getChildByTag(65535);并通过强制类型转换获取这个文字

	return true;
}

//开始触摸
void HelloWorld::ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent){
	sprite_num++;//触摸屏幕生成一个按钮精灵
	CCSprite *sprite=CCSprite::create("CloseSelected.png");  
	CCTouch *touch=(CCTouch *)pTouches->anyObject();//获取触摸
	CCLabelTTF *label=(CCLabelTTF*)this->getChildByTag(LABEL_TARGET);//获取文字

	label->setString(CCString::createWithFormat("Touch Begin\nsprite %d:%.2f,%.2f",sprite_num,touch->getLocation().x,touch->getLocation().y)->getCString());
	//显示当前点击位置,点击位置的坐标通过touch->getLocation().x/y来获取

	sprite->setPosition(ccp(touch->getLocation().x,touch->getLocation().y));//按钮精灵的位置放置在触摸的位置
	this->addChild(sprite,0,sprite_num);//生成精灵的Tag为当前按钮精灵的数量。

};
//触摸并移动
void HelloWorld::ccTouchesMoved(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent){
	CCTouch *touch=(CCTouch *)pTouches->anyObject();//获取触摸
	CCLabelTTF *label=(CCLabelTTF*)this->getChildByTag(LABEL_TARGET);//获取文字

	label->setString(CCString::createWithFormat("Touch Move\nsprite %d:%.2f,%.2f",sprite_num,touch->getLocation().x,touch->getLocation().y)->getCString());
	//显示当前点击位置,点击位置的坐标通过touch->getLocation().x/y来获取

	CCSprite *sprite=(CCSprite*)this->getChildByTag(sprite_num);//获取精灵
	sprite->setPosition(ccp(touch->getLocation().x,touch->getLocation().y));//设置精灵的位置
};
//结束触摸,同上
void HelloWorld::ccTouchesEnded(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent){
	CCTouch *touch=(CCTouch *)pTouches->anyObject();
	CCLabelTTF *label=(CCLabelTTF*)this->getChildByTag(LABEL_TARGET);
	label->setString(CCString::createWithFormat("Touch End\nsprite %d:%.2f,%.2f",sprite_num,touch->getLocation().x,touch->getLocation().y)->getCString());
	CCSprite *sprite=(CCSprite*)this->getChildByTag(sprite_num);
	sprite->setPosition(ccp(touch->getLocation().x,touch->getLocation().y))	;
};
//触摸被强行打断
void HelloWorld::ccTouchesCancelled(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent){
};


此程序的关键地方如下:

(1)在此场景的初始化过程中,先初始化一个没有内容的文字,通过宏,定义其TARGET为65535,用于显示触摸坐标,之所以定义一个这么大的TARGET,是因为没触摸屏幕一次,则在屏幕上生成一个带TARGET的精灵。精灵1的TARGET就为1,精灵2的TARGET就为2,以此类推,避免精灵的TARGET与文字的TARGET重复。

(2)只要在bool HelloWorld::init(){}存在语句this->setTouchEnabled(true);,所有触摸处理函数皆与场景HelloWorld中生效。程序猿只要实现这四个函数即可。

(3)改变精灵位置,直接通过setPosition方法实现即可,无须把此精灵删除又添加,也不要通过《【Cocos2dx】基本动作、动作序列与动作合并》(点击打开链接)中的MoveTo动作,设置一个0.0f的时间间隔实现,Cocos2dx的动作皆带动画效果,很耗费资源,而且视觉效果会错位。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: