cocos2d-x 3.x中Layer层触摸事件

前端之家收集整理的这篇文章主要介绍了cocos2d-x 3.x中Layer层触摸事件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


简单实现监听layer(this)的3个触摸事件。


///触摸事件的处理

	this->setTouchEnabled(true);
	auto dispatcher = Director::getInstance()->getEventDispatcher();        
	auto listener = EventListenerTouchOneByOne::create();       

	listener->onTouchBegan = [](Touch* touch,Event* event){
		CCLOG("touch began");
		return true;//一定要返回true ,否则后面的事件会监听不到
	};
	listener->onTouchMoved = [](Touch* touch,Event* event){
		CCLOG("touch moved");
		Vec2 v = touch->getLocation();
		CCLOG("%f %f",v.x,v.y);

	};
	listener->onTouchEnded = [=](Touch* touch,Event* event){
		CCLOG("touch ended");
	};
	listener->setSwallowTouches(true);//不向下传递触摸       
	dispatcher->addEventListenerWithSceneGraPHPriority(listener,this);

猜你在找的Cocos2d-x相关文章