今天。本人写一个小游戏,由于 要判断,,屏幕是从那边滑过来的,所以 有一个小小的逻辑判断,废话不多说了。直接上代码,代码有解释。
bool StartScene::ccTouchBegan(CCTouch *touch, CCEvent *event)
{
//获取触摸的X轴和Y轴
CCPoint touchPoint = touch->getLocation(); //获取OpenGL坐标(即cocos2d-x坐标,原点在左下角)
touch->getLocationInView();
firstX = touchPoint.x;
firstY = touchPoint.y;
return true;
}
void StartScene::ccTouchMoved(CCTouch *touch, CCEvent *event)
{
return;
}
void StartScene::ccTouchEnded(CCTouch *touch, CCEvent *event)
{
//获取X轴和Y轴的移动范围
CCPoint touchPoint = touch->getLocation(); //获取OpenGL坐标(即cocos2d-x坐标,原点在左下角)
endX = firstX - touchPoint.x;
endY = firstY - touchPoint.y;
touchx = touchPoint.x;
touchy = touchPoint.y;
CCLog("firstX %d firstY %d touchx %d touchy %d", firstX, firstY, touchx, touchy);
CCLog(" firstX - touchPoint.x %d firstY - touchPoint.y %d", endX, endY);
//判断X轴和Y轴的移动距离,如果X轴的绝对值大,则向左右滑动,如果Y轴的绝对值大,则向上下滑动
if (abs(endX) > abs(endY))
{
//手势向左右
//判断向左还是向右
if (endX > 0)
{
//向左函数
doLeft();
}
else
{
//向右函数
doRight();
}
}
else
{
//手势向上下
//判断手势向上还是向下
if (endY > 0)
{
//向下函数
doDown();
}
else
{
//向上函数
doUp();
}
}
}
//逻辑是这样子的。。触摸的开始的时候,,,获取 第一次点击的坐标。。。然后 结束的时候。获取到最后一次的点击 坐标点。。。 自己最好弄张纸。。画好坐标 自己算算。。~