9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Snippets]スワイプ判定

Posted at
enum定義

enum kSwipeDirection{
   kSwipeRight = 1234,
   kSwipeLeft,
   kSwipeDown,
   kNotSwipeTouch,
};

判定メソッドの実装
//スワイプ判定を行う
kSwipeDirection GameScene::getSwipeDirection(Point first ,Point last) {
   //距離の計算
   //スレッショルドとなる距離以上のスワイプが行われているかを測定する
   int distance = sqrt((first.x-last.x)*(first.x-last.x) + (first.y-last.y)*(first.y-last.y));
   double rad =atan((last.y-first.y)/(last.x-first.x));
   //スレッショルドを50pxにする
   if (distance > 100) {
      if (first.x < last.x) {
         if ( -M_PI/6<rad && rad < M_PI/6 ){
            return kSwipeRight;
         }
      }
      if (first.x > last.x) {
         if ( -M_PI/6<rad && rad < M_PI/6 ){
            return kSwipeLeft;
         }
      }
   }
   if (distance > 70) {
      if (first.y >last.y) {
         if (-2*M_PI/3 < rad && rad < -M_PI/3) {
            return kSwipeDown;
         }
      }
   }
   return kNotSwipeTouch;
}

最初のポイントはonTouchBegan,最後のポイントはonTouchEndedで取得しておくこと。そして、スワイプの方向の情報を取ったあとは、
一応初期化しておく事

 kSwipeDirection direction;
   direction = getSwipeDirection(firstTouchPoint, lastTouchPoint);
//メンバ変数の初期化
   firstTouchPoint = Point::ZERO;
   lastTouchPoint = Point::ZERO;
タッチポイントはgetLocationでの取得で良いが、DesignResolutionと、OpenGL上(実際の座標)に大きな差が開いたら端末差が大きくなるので、その場合はgetLocationInViewで取ってscalefactorで値調整するなど,考えると良い。
9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?