博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
EventListenerTouchOneByOne::create() 单点触摸
阅读量:4981 次
发布时间:2019-06-12

本文共 2445 字,大约阅读时间需要 8 分钟。

void HelloWorld::onEnter(){	Layer::onEnter();	log("HelloWorld onEnter");	// 创建一个事件监听器 OneByOne 为单点触摸	auto listener = EventListenerTouchOneByOne::create();	// 设置是否吞没事件,在 onTouchBegan 方法返回 true 时吞没	listener->setSwallowTouches(true);	// onTouchBegan 事件回调函数	listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::touchBegan, this);	// onTouchMoved 事件回调函数	listener->onTouchMoved =  CC_CALLBACK_2(HelloWorld::touchMoved, this);	// onTouchEnded 事件回调函数	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));}bool HelloWorld::touchBegan(Touch* touch, Event* event){	// 获取事件所绑定的 target 	auto target = static_cast
(event->getCurrentTarget()); // 获取当前点击点所在相对按钮的位置坐标 Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation()); Size s = target->getContentSize(); Rect rect = Rect(0, 0, s.width, s.height); // 点击范围判断检测 if (rect.containsPoint(locationInNode)) { log("sprite x = %f, y = %f ", locationInNode.x, locationInNode.y); log("sprite tag = %d", target->getTag()); target->runAction(ScaleBy::create(0.06f, 1.06f)); return true; } return false;}void HelloWorld::touchMoved(Touch *touch, Event *event){ log("onTouchMoved"); auto target = static_cast
(event->getCurrentTarget()); // 移动当前按钮精灵的坐标位置 target->setPosition(target->getPosition() + touch->getDelta());}void HelloWorld::touchEnded(Touch *touch, Event *event){ log("onTouchEnded"); auto target = static_cast
(event->getCurrentTarget()); log("sprite onTouchesEnded.. "); Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation()); Size s = target->getContentSize(); Rect rect = Rect(0, 0, s.width, s.height); // 点击范围判断检测 if (rect.containsPoint(locationInNode)) { log("sprite x = %f, y = %f ", locationInNode.x, locationInNode.y); log("sprite tag = %d", target->getTag()); target->runAction(ScaleTo::create(0.06f, 1.0f)); }}void HelloWorld::onExit(){ Layer::onExit(); log("HelloWorld onExit"); Director::getInstance()->getEventDispatcher()->removeAllEventListeners();}

转载于:https://www.cnblogs.com/Anzhongliu/p/6091937.html

你可能感兴趣的文章
08-Location总结图解
查看>>
python装饰器使用及理解
查看>>
JAVA规则引擎JSR-94笔札
查看>>
Azure IOT (EventHub + Stream Analytics + Table Storage)的使用
查看>>
centos7安装lamp
查看>>
ASP.NET Core 1.0中的管道-中间件模式
查看>>
Docker 安装mysql以及外部访问
查看>>
spring boot中yml配置文件里横线的意思
查看>>
Numpy——进阶篇
查看>>
Django - Xadmin 组件(一)
查看>>
LeetCode OJ 之 Number of Digit One (数字1的个数)
查看>>
也谈如何获取真实正确的 Windows 系统版本号
查看>>
git基本操作
查看>>
Web for Pentester -- Directory traversal
查看>>
1.jdk安装和环境配置
查看>>
用caffe训练openpose过程中,出现异常
查看>>
POJ-2503 Babelfish Hash表
查看>>
java连接ibm mq
查看>>
oracle常用的符号用法
查看>>
理解爬虫原理
查看>>