简单实现监听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);


Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐