在上一篇博客介绍了在Cocos2d-X3.0中实现单点触摸,但是有些游戏还会用到多点触摸,其中最典型的的游戏室节奏大师,在节奏大师中会不断产生运动的音符,玩家需要不停的点击音符以获得高分,而且玩家可以多个手指头一起点,多个手指头一起点就是使用多点触摸实现的
下面通过一个小的例子介绍如何在Cocos2d-X3.0中实现多点触摸
首先创建一个touches类,并且在touches.h中添加下面的代码
#ifndef _touches_H_#define _touches_H_#include "cocos2d.h"USING_NS_CC;class touches : public Layer{public: touches(voID); ~touches(voID); CREATE_FUNC(touches); static Scene* createScene(); bool init(); //开始触摸 voID ontouchesBegan(const std::vector<touch*>& touches,Event *event); //滑动 voID ontouchesMoved(const std::vector<touch*>& touches,Event *event); //结束触摸 voID ontouchesEnded(const std::vector<touch*>& touches,Event *event); //取消触摸 voID ontouchesCancelled(const std::vector<touch*>& touches,Event *event);};#endif
在touches.cpp中添加下面的代码
#include "touches.h"touches::touches(voID){}touches::~touches(voID){}Scene* touches::createScene(){ auto scene = Scene::create(); auto layer = touches::create(); scene->addChild(layer); return scene;}bool touches::init(){ if(!Layer::init()) { return false; } //创建一个事件监听器,AllAtOne为多点触摸 auto Listener = EventListenertouchAllAtOnce::create(); //事件回调函数 Listener->ontouchesBegan = CC_CALLBACK_2(touches::ontouchesBegan,this); Listener->ontouchesMoved = CC_CALLBACK_2(touches::ontouchesMoved,this); Listener->ontouchesEnded = CC_CALLBACK_2(touches::ontouchesEnded,this); //添加监听器 _eventdispatcher->addEventListenerWithSceneGraPHPriority(Listener,this); return true;}//开始触摸voID touches::ontouchesBegan(const std::vector<touch*>& touches,Event *event){ log("touches began !");} //滑动voID touches::ontouchesMoved(const std::vector<touch*>& touches,Event *event){ log("touches moved !");} //结束触摸voID touches::ontouchesEnded(const std::vector<touch*>& touches,Event *event){ log("touches enaded !");}//取消触摸voID touches::ontouchesCancelled(const std::vector<touch*>& touches,Event *event){ ontouchesEnded(touches,event);}
程序测试的结果:
当触摸屏幕时会打印touches began !
当触摸结束后会打印touches ended !
当在屏幕上滑动时会打印touches moved !
总结
以上是内存溢出为你收集整理的在Cocos2d-X3.0中实现多点触摸全部内容,希望文章能够帮你解决在Cocos2d-X3.0中实现多点触摸所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)