今天。本人寫一個小遊戲,由於 要判斷,,屏幕是從那邊滑過來的,所以 有一個小小的邏輯判斷,廢話不多說了。直接上代碼,代碼有解釋。
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();
}
}
}
//邏輯是這樣子的。。觸摸的開始的時候,,,獲取 第一次點擊的座標。。。然後 結束的時候。獲取到最後一次的點擊 座標點。。。 自己最好弄張紙。。畫好座標 自己算算。。~