xxx.h文件:
bool onMapTouchBegan(cocos2d::Touch *touch,cocos2d::Event *event);
void onMapTouchMove(cocos2d::Touch *touch,cocos2d::Event *event);
void onMapTouchEnded(cocos2d::Touch *touch,cocos2d::Event *event);
xxx.cpp文件:
//1.单点触摸
auto mapOneByOne = EventListenerTouchOneByOne::create();
mapOneByOne->setSwallowTouches(true);//其它事件都吞没掉(点击一个对象,其它对象的事件是否被吞没)
mapOneByOne->onTouchBegan = CC_CALLBACK_2(MainMap::onMapTouchBegan,this);
mapOneByOne->onTouchMoved = CC_CALLBACK_2(MainMap::onMapTouchMove,this);
mapOneByOne->onTouchEnded = CC_CALLBACK_2(MainMap::onMapTouchEnded,this);
//添加触摸事件
_eventDispatcher->addEventListenerWithSceneGraPHPriority(mapOneByOne,imgMap);
bool MainMap::onMapTouchBegan(cocos2d::Touch * touch,cocos2d::Event * event)
{
CCLOG("onMapTouchBegan");
return true;
}
void MainMap::onMapTouchMove(cocos2d::Touch * touch,cocos2d::Event * event)
{
CCLOG("onMapTouchMove");
//手指滑动的位置距离
Vec2 diff = touch->getDelta();
//地图原来的位置
Vec2 posSrc = imgMap->getPosition();
//滑动后的位置
Vec2 posDes = posSrc + diff;
//判断不能拖到屏幕以外
if (posDes.x >= 1280) {
posDes.x = 1280;
}
if (posDes.y >= 814) {
posDes.y = 814;
}
if (posDes.x <= (960 - 1280)) {
posDes.x = 960 - 1280;
}
if (posDes.y <= (640 - 814)) {
posDes.y = 640 - 814;
}
//图片滑动后的位置
//imgMap->setPosition(posDes);
}
void MainMap::onMapTouchEnded(cocos2d::Touch * touch,cocos2d::Event * event)
{
CCLOG("onMapTouchEnded");
}