0
0

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.

cocos2d-x Gesture - 터치 제스처 인식기

Last updated at Posted at 2019-06-29

cocos2d-x 프로젝트에 터치 제스처가 필요해서 구현을 해볼까 고민했지만,
이번에도 훌륭한 샘플 소스를 발견하여 쉽게 적용할 수 있었습니다.

github 샘플 소스 -> Cocos2d-x Gesture Recognizers


  • 소스 경로

샘플 소스의 Gestures폴더를 Classes에 넣습니다.
Classes/Gestures


  • CMakeLists.txt 소스 추가
CMakeLists.txt
# add cross-platforms source files and header files 
list(APPEND GAME_SOURCE
     Classes/PanScene.cpp
     Classes/PinchScene.cpp
     Classes/Gestures/GestureRecognizer.cpp
     Classes/Gestures/GestureRecognizerUtils.cpp
     Classes/Gestures/LongPressGestureRecognizer.cpp
     Classes/Gestures/PanGestureRecognizer.cpp
     Classes/Gestures/PinchGestureRecognizer.cpp
     Classes/Gestures/SwipeGestureRecognizer.cpp
     Classes/Gestures/TapGestureRecognizer.cpp
)

list(APPEND GAME_HEADER
Classes/PanScene.h
Classes/PinchScene.h
Classes/Gestures/GestureRecognizer.h
Classes/Gestures/GestureRecognizerUtils.h
Classes/Gestures/LongPressGestureRecognizer.h
Classes/Gestures/PanGestureRecognizer.h
Classes/Gestures/PinchGestureRecognizer.h
Classes/Gestures/SwipeGestureRecognizer.h
Classes/Gestures/TapGestureRecognizer.h
)

  • 구현

샘플 소스의 test폴더에 테스트 소스가 있습니다.
필요한 기능의 클래스를 보고 그대로 적용하면 됩니다.
저는 PanScene - 이동, PinchScene - 이동 및 확대/축소/회전을 적용했습니다.

include.h
#include "PinchScene.h"
#include "PanScene.h"

제스처가 필요한 부분에서 초기화합니다.
제 프로젝트에서는 사진을 불러온 후 제스처를 사용합니다.

void PhotoCtrl::initGestureRecognizer()
{
    //
    // pinch gesture
    auto pins = PinchScene::createScene();
    PinchScene* pinch = static_cast<PinchScene*>(pins->getChildByName("pinch"));
    pinch->setSprite(_sprite); // _sprite에 제스처가 적용됩니다. 
    addChild(pins);
    
    //
    // pan gesture
    auto pans = PanScene::createScene();
    PanScene* pan = static_cast<PanScene*>(pans->getChildByName("pan"));
    pan->setSprite(_sprite);
    addChild(pans);
}

PanScene에서 손가락 수 설정

void PanScene::addGestureRecognizers()
{
    //float fingers = 3;
    float fingers = 1; // 한 손가락으로도 사진을 이동시킬 수 있도록 할 수 있습니다. 
    auto pan = PanGestureRecognizer::create(fingers);
    pan->onPan = CC_CALLBACK_1(PanScene::onPan, this);
    addChild(pan);
}
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?