cocos2dx 2.1.5版本的手势识别

来源:互联网 发布:美工助理是做什么的 编辑:程序博客网 时间:2024/06/03 16:32

看2048的教学,看到手势识别这一块,突然发现问题,就是2.0跟3.0版本的类不同,他有监听函数而我的版本没有,查了半天资料才找到解决办法,特此记录下来:


第一步:

在init方法中,添加设置可触摸

setTouchEnabled(true);

第二部:

重写函数

void HelloWorld::registerWithTouchDispatcher()
{
CCDirector* director = CCDirector::sharedDirector();
director->getTouchDispatcher() ->addTargetedDelegate(this,0,true);
}

bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *touch,cocos2d::CCEvent *event)
{
CCLog("TouchBegin");
CCPoint touchPoint = touch->getLocation();//获取点击位置
firstX = touchPoint.x;
firstY = touchPoint.y;
return true;
}
void HelloWorld::ccTouchEnded(cocos2d::CCTouch *touch,cocos2d::CCEvent *event)
{
CCLog("TouchEnded");
//获取X轴跟y轴的移动范围
CCPoint touchPoint = touch->getLocation();//获取点击位置
endX = firstX-touchPoint.x;
endY = firstY-touchPoint.y;
//判断移动方向,比较x的绝对值和y的绝对值大小
if(abs(endX)>abs(endY)){
//左右
if(endX+5>0){
//左
doLeft();
}
else{
//右
doRight();
}
}
else{
//上下
if(endY+5>0){
//上
doUp();
}
else{
//下
doDown();
}
}
}

0 0
原创粉丝点击