Cocos2d-x处理Touch触摸事件Demo

举报
yd_221104950 发表于 2020/12/02 23:56:01 2020/12/02
【摘要】 请参考《Ubuntu18.04搭建Cocos2d开发环境》搭建项目 默认情况下,Cocos2d-x会将鼠标左点击当作触摸。 第一步:在/home/kyun/Desktop/Games/MyGame/CMakeLists.txt文件添加TouchScene.h和TouchScene.cpp: # add cross-platforms source files an...

请参考《Ubuntu18.04搭建Cocos2d开发环境》搭建项目

默认情况下,Cocos2d-x会将鼠标左点击当作触摸。

第一步:在/home/kyun/Desktop/Games/MyGame/CMakeLists.txt文件添加TouchScene.h和TouchScene.cpp:

# add cross-platforms source files and header files 
list(APPEND GAME_SOURCE Classes/AppDelegate.cpp Classes/TouchScene.cpp )
list(APPEND GAME_HEADER Classes/AppDelegate.h Classes/TouchScene.h )

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

第二步:编写TouchScene.h和TouchScene.cpp

TouchScene.h:


#include "cocos2d.h"

class TouchScene : public cocos2d::Layer {
public: static cocos2d::Scene* createScene(); virtual  bool init();
	//Touch事件回调方法 virtual bool onTouchBegan(cocos2d::Touch*,cocos2d::Event*); virtual void onTouchEnded(cocos2d::Touch*,cocos2d::Event*); virtual void onTouchMoved(cocos2d::Touch*,cocos2d::Event*); virtual void onTouchCancelled(cocos2d::Touch*,cocos2d::Event*); CREATE_FUNC(TouchScene);
private: cocos2d::Label* labelTouchInfo;

};

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

TouchScene.cpp:

//
// Created by kyun on 20-3-12.
//

#include "TouchScene.h"

USING_NS_CC;


Scene* TouchScene::createScene() { auto scene = Scene::create(); auto layer = TouchScene::create(); scene->addChild(layer); return scene;
}

bool TouchScene::init() { if(!Layer::init()){ return false; }
	// Label标签 labelTouchInfo = Label::createWithSystemFont("Touch or clicksomewhere to begin","Arial",30); labelTouchInfo->setPosition(Vec2(Director::getInstance()->getVisibleSize().width/2,Director::getInstance()->getVisibleSize().height/2)); // 创建EventListenerTouchOneByOne类型的事件监听器 auto touchListener = EventListenerTouchOneByOne::create(); // 将处理函数绑定到事件监听器上 touchListener->onTouchBegan = CC_CALLBACK_2(TouchScene::onTouchBegan,this); touchListener->onTouchEnded = CC_CALLBACK_2(TouchScene::onTouchEnded,this); touchListener->onTouchMoved = CC_CALLBACK_2(TouchScene::onTouchMoved,this); touchListener->onTouchCancelled = CC_CALLBACK_2(TouchScene::onTouchCancelled,this); // 将事件监听器注册到节点的_eventDispatcher上 _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener,this); this->addChild(labelTouchInfo); return true;
}

bool TouchScene::onTouchBegan(cocos2d::Touch * touch, cocos2d::Event * event) { labelTouchInfo->setPosition(touch->getLocation()); labelTouchInfo->setString("You touched here"); return true;

}

void TouchScene::onTouchEnded(cocos2d::Touch * touch, cocos2d::Event * event) { cocos2d::log("touch ended");
}

void TouchScene::onTouchMoved(cocos2d::Touch *touch, cocos2d::Event * event) { cocos2d::log("touch moved");
}

void TouchScene::onTouchCancelled(cocos2d::Touch *touch, cocos2d::Event *event) { cocos2d::log("touch cancelled");
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

第三步:修改AppDelegate.cpp

#include "TouchScene.h"
...
bool AppDelegate::applicationDidFinishLaunching() { ... // create a scene. it's an autorelease object
	auto scene = TouchScene::createScene(); // run director->runWithScene(scene); return true;
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

最后,运行就可以了。

分析TouchScene中的用到的方法

  • 关于EventListenerTouchOneByOne事件监听器处理函数的定义
class CC_DLL EventListenerTouchOneByOne : public EventListener
{ public: typedef std::function<bool(Touch*, Event*)> ccTouchBeganCallback; typedef std::function<void(Touch*, Event*)> ccTouchCallback; ccTouchBeganCallback onTouchBegan; ccTouchCallback onTouchMoved; ccTouchCallback onTouchEnded; ccTouchCallback onTouchCancelled;
... };


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

我们根据此来定义我们的函数,上面规定了处理函数的返回值形参顺序。其实我们也可以使用Lambda表达式来定义处理函数,如:

touchListener->onTouchBegan = [](Touch* touch,Event* event)->bool{
// todo
}

  
 
  • 1
  • 2
  • 3

完整的例子,我们修改init方法,让其使用lambda表达式来定义处理函数:

//
// Created by kyun on 20-3-12.
//

#include "TouchScene.h"

USING_NS_CC;


Scene* TouchScene::createScene() { auto scene = Scene::create(); auto layer = TouchScene::create(); scene->addChild(layer); return scene;
}

bool TouchScene::init() { if(!Layer::init()){ return false; } auto sprite = Sprite::create("logo2.png"); sprite->setAnchorPoint(Vec2(0.5,0.5)); auto visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); sprite->setPosition(Vec2(visibleSize.width/2+origin.x,visibleSize.height/2+origin.y)); auto touchListener = EventListenerTouchOneByOne::create(); touchListener->onTouchBegan = [](Touch* touch,Event* event)->bool{ auto bounds = event->getCurrentTarget()->getBoundingBox(); if(bounds.containsPoint(touch->getLocation())){ std::stringstream touchDetails; touchDetails << "Touched at OpenGL coordinates:" <<touch->getLocation().x << "," << touch->getLocation().y << std::endl<< "Touched at UI coordinate:"<<touch->getLocationInView().x << "," << touch->getLocationInView().y << std::endl << "Touched at local coordinate:" << event->getCurrentTarget()->convertToNodeSpace(touch->getLocation()).x << "," << event->getCurrentTarget()->convertToNodeSpace(touch->getLocation()).y << std::endl<< "Touch moved by:" << touch->getDelta().x << "," << touch->getDelta().y; ccMessageBox(touchDetails.str().c_str(),"Touched"); } return true; }; // Add a "touch" event listener to our sprite Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener,sprite); this->addChild(sprite,0); this->addChild(labelTouchInfo); return true;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

上面的效果就是点sprite后会弹出一个对话框。

  • ccMessageBox()弹窗
  • touch->getLocationInView()返回在屏幕坐标系(原点在屏幕左上角)中的位置
  • touch->getLocation()返回在OpenGL坐标系(原点在屏幕左下角)中的位置

谢谢阅读

文章来源: blog.csdn.net,作者:WongKyunban,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/weixin_40763897/article/details/104822927

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。