为了让大家掌握Cocos2d-x中的事件机制,下面我们以触摸事件为例,使用事件触发器实现单点触摸事件。该实例如图8-3所示,场景中有三个方块精灵,显示顺序如图8-3所示,拖拽它们可以移动它们,事件响应优先级是按照它们的显示顺序。
下面我们再看看具体的程序代码,首先看一下HelloWorldScene.h文件,它的代码如下:
- #ifndef __HELLOWORLD_SCENE_H__
- #define __HELLOWORLD_SCENE_H__
- #include "cocos2d.h"
- typedef enum ①
- {
- kBoxA_Tag = 102
- ,kBoxB_Tag
- ,kBoxC_Tag
- } SpriteTags; ②
- class HelloWorld : public cocos2d::Layer
- {
- public:
- static cocos2d::Scene* createScene();
- virtual bool init();
- virtualvoid onEnter(); ③
- virtualvoid onExit(); ④
- booltouchBegan(cocos2d::Touch* touch, cocos2d::Event* event); ⑤
- void touchMoved(cocos2d::Touch *touch, cocos2d::Event *event); ⑥
- void touchEnded(cocos2d::Touch *touch, cocos2d::Event *event); ⑦
- CREATE_FUNC(HelloWorld);
- };
- #endif // __HELLOWORLD_SCENE_H__
上述代码第①~②行是定义个枚举类型SpriteTags,枚举类型SpriteTags中定义了三个常量,这三个常量对应场景中的三个精灵的标签(Tag)属性。代码第③行声明了层声明周期的onEnter()函数,我们将在该函数中注册监听器和初始化设置。第④行代码是声明了层声明周期的onExit()函数,我们将在该函数中注销监听器和释放一些资源。代码第⑤~⑥行是声明单点触摸事件回调函数。
HelloWorldScene的实现代码HelloWorldScene.ccp文件,它的HelloWorld::init()代码如下:
- bool HelloWorld::init()
- {
- if( !Layer::init() )
- {
- returnfalse;
- }
- SizevisibleSize = Director::getInstance()->getVisibleSize();
- Pointorigin = Director::getInstance()->getVisibleOrigin();
- //贴图的纹理图片宽高必须是2的n次幂,128x128
- autobg = Sprite::create("BackgroundTile.png",
- Rect(0,0, visibleSize.width, visibleSize.height)); ①
- //贴图的纹理参数,水平重复平铺,垂直重复平铺
- Texture2D::TexParamstp = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT}; ②
- bg->getTexture()->setTexParameters(tp); ③
- bg->setPosition(origin+ Point(visibleSize.width/2, visibleSize.height/2));
- addChild(bg,0); ④
- Sprite*boxA = Sprite::create("BoxA2.png"); ⑤
- boxA->setPosition(origin+Point(visibleSize.width/2,visibleSize.height/2) + Point(-120, 120));
- addChild(boxA,10, kBoxA_Tag);
- Sprite*boxB = Sprite::create("BoxB2.png");
- boxB->setPosition(origin+Point(visibleSize.width/2,visibleSize.height/2));
- addChild(boxB,20, kBoxB_Tag);
- Sprite*boxC = Sprite::create("BoxC2.png");
- boxC->setPosition(origin+Point(visibleSize.width/2,visibleSize.height/2) + Point(120, 160));
- addChild(boxC,30, kBoxC_Tag); ⑥
- returntrue;
- }
我们在HelloWorld::init()函数中初始化了场景中的背景和三个方块精灵。代码第①~④行是创建并添加背景,图8-3所示的背景是由一个128x128纹理图片(BackgroundTile.png)反复贴图上,这样可以减少内存消耗,在第①行代码中创建背景精灵对象,注意背景的大小仍然是整个屏幕。第②行代码是设置贴图的纹理的参数,Texture2D::TexParams类型是一个结构体。第③行代码是将参数设置到背景精灵的纹理上。第④行代码是添加背景精灵到当前层。
代码第⑤~⑥行是创建了三个方块精灵,在添加它到当前层的时候我们使用三个参数的addChild(Node* child,int localZOrder,int tag)函数,这样可以通过localZOrder参数指定精灵的显示顺序。
- HelloWorldScene.ccp中的HelloWorld::onEnter()代码如下:
- void HelloWorld::onEnter()
- {
- Layer::onEnter();
- log("HelloWorldonEnter");
- autolistener = EventListenerTouchOneByOne::create(); ①
- listener->setSwallowTouches(true); ②
- listener->onTouchBegan= CC_CALLBACK_2(HelloWorld::touchBegan, this); ③
- listener->onTouchMoved= CC_CALLBACK_2(HelloWorld::touchMoved,this); ④
- listener->onTouchEnded= CC_CALLBACK_2(HelloWorld::touchEnded,this); ⑤
- //添加监听器
- EventDispatcher*eventDispatcher = Director::getInstance()->getEventDispatcher(); ⑥
- eventDispatcher->addEventListenerWithSceneGraphPriority(listener,
- getChildByTag(kBoxA_Tag)); ⑦
- eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(),
- getChildByTag(kBoxB_Tag)); ⑧
- eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(),
- getChildByTag(kBoxC_Tag)); ⑨
- }
上述代码第①行是创建一个单点触摸事件监听器对象。第②行代码是设置是否吞没事件,如果设置为true,那么在onTouchBegan函数返回 true 时吞没事件,事件不会传递给下一个Node对象。第③行代码是设置监听器的onTouchBegan属性回调函数。第④行代码是设置监听器的onTouchMoved属性回调函数。第⑤行代码是设置监听器的onTouchEnded属性回调函数。
代码第⑥~⑨行是添加监听器,其中第⑦行使用精灵显示优先级添加事件监听器,其中参数getChildByTag(kBoxA_Tag)是通过精灵标签Tag实现获得精灵对象。第⑧行和第⑨行代码是为另外两精灵添加事件监听器,其中listener->clone()获得listener对象,使用clone()函数是因为每一个事件监听器只能被添加一次,addEventListenerWithSceneGraphPriority和addEventListenerWithFixedPriority会在添加事件监听器时设置一个注册标识,一旦设置了注册标识,该监听器就不能再用于注册其它事件监听了,因此我们需要使用listener->clone()克隆一个新的监听器对象,把这个新的监听器对象用于注册。
HelloWorldScene.ccp中的触摸事件回调函数代码如下:
- bool HelloWorld::touchBegan(Touch*touch, Event* event) ①
- {
- //获取事件所绑定的 target
- autotarget = static_cast<Sprite*>(event->getCurrentTarget()); ②
- PointlocationInNode = target->convertToNodeSpace(touch->getLocation());