EventListenerTouchOneByOne :单点触控
std::function onTouchBegan; //触摸开始事件
std::function onTouchMoved; //触摸移动事件
std::function onTouchEnded; //触摸结束事件
std::function onTouchCancelled; //触摸中断事件
EventListenerTouchAllAtOnce:多点触控
std::function&, Event*)> onTouchesBegan; //触摸开始事件
std::function&, Event*)> onTouchesMoved; //触摸移动事件
std::function&, Event*)> onTouchesEnded; //触摸结束事件
std::function&, Event*)> onTouchesCancelled; //触摸中断事件
bool TouchTest::init()
{
if ( !Layer::init() )
{
return false;
}
//允许接收触摸事件
this->setTouchEnabled(true);
EventDispatcher* eventDispatcher = Director::getInstance()->getEventDispatcher();
auto listen = EventListenerTouchAllAtOnce::create();
listen->onTouchesBegan = CC_CALLBACK_2(TouchTest::onTouchesBegan,this);
listen->onTouchesMoved = CC_CALLBACK_2(TouchTest::onTouchesMoved,this);
listen->onTouchesEnded = CC_CALLBACK_2(TouchTest::onTouchesEnded,this);
listen->onTouchesCancelled = CC_CALLBACK_2(TouchTest::onTouchesCancelled,this);
eventDispatcher->addEventListenerWithSceneGraphPriority(listen,this);
return true;
}
//触摸事件开始,手指按下时
void TouchTest::onTouchesBegan(const std::vector& touches, cocos2d::Event *event)
{
for(auto &item : touches)
{
auto touch = item;
auto pos1 = touch->getLocation();
auto pos2 = touch->getLocationInView();
auto pos3 = Director::getInstance()->convertToUI(pos2);
log("pos1 x: %f, y: %f",pos1.x,pos1.y);
log("pos2 x: %f, y: %f",pos2.x,pos2.y);
log("pos2 x: %f, y: %f",pos3.x,pos3.y);
}
}
//触摸移动事件,也就是手指在屏幕滑动的过程
void TouchTest::onTouchesMoved(const std::vector& touches, cocos2d::Event *event)
{
log("TouchTest onTouchesMoved");
}
//触摸事件结束,也就是手指松开时
void TouchTest::onTouchesEnded(const std::vector& touches, cocos2d::Event *event)
{
log("TouchTest onTouchesEnded");
}
//打断触摸事件,一般是系统层级的消息,如来电话,触摸事件就会被打断
void TouchTest::onTouchesCancelled(const std::vector& touches, cocos2d::Event *event)
{
log("TouchTest onTouchesCancelled");
}
setTouchEnabled(); //设置是否允许接收触摸事件
获取单击屏幕时的坐标方式:(1)touch->getLocation(); 获得单击坐标,基于3D
(2)touch->getLocationInView(); 获取单击坐标,是屏幕坐标系的坐标
(3)Director::getInstance()->convertToUI(pos2); //将屏幕坐标系的坐标,转为cocos2dx的坐标
坐标系:cocos2d-x是基于openGLES的,所以遵循openGL的坐标系,也就是说是以屏幕的左下角为坐标原点。屏幕坐标系一般是以左下角为坐标原点。
//设置多点触控
this->setTouchMode(Touch::DispatchMode::ALL_AT_ONCE);
//设置单点触控
//this->setTouchMode(Touch::DispatchMode::ONE_BY_ONE);
单点触控触摸监听代码:
this->setTouchMode(Touch::DispatchMode::ONE_BY_ONE);
auto oneTouch = EventListenerTouchOneByOne::create();
oneTouch->onTouchBegan = CC_CALLBACK_2(TouchTest::onTouchBegan,this);
oneTouch->onTouchMoved = CC_CALLBACK_2(TouchTest::onTouchMoved,this);
oneTouch->onTouchEnded = CC_CALLBACK_2(TouchTest::onTouchEnded,this);
oneTouch->onTouchCancelled = CC_CALLBACK_2(TouchTest::onTouchCancelled,this);
eventDispatcher->addEventListenerWithSceneGraphPriority(oneTouch,this);
//设置多点触控
this->setTouchMode(Touch::DispatchMode::ALL_AT_ONCE);
auto listen = EventListenerTouchAllAtOnce::create();
listen->onTouchesBegan = CC_CALLBACK_2(TouchTest::onTouchesBegan,this);
listen->onTouchesMoved = CC_CALLBACK_2(TouchTest::onTouchesMoved,this);
listen->onTouchesEnded = CC_CALLBACK_2(TouchTest::onTouchesEnded,this);
listen->onTouchesCancelled = CC_CALLBACK_2(TouchTest::onTouchesCancelled,this);
eventDispatcher->addEventListenerWithSceneGraphPriority(listen,this);
listen->setSwallowTouches(true);
setSwallowTouches用于设置是否吞没事件,也就是当某个触摸事件回调的时,截断该事件,让它不能继续传递给其他人。