멀티터치 이벤트 설정하기



hpp)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef test179_hpp
#define test179_hpp
 
USING_NS_CC;
 
class Test179 : public cocos2d::Layer
{
public:
    static cocos2d::Scene* createScene();
    
    virtual bool init();
    
    // implement the "static create()" method manually
    CREATE_FUNC(Test179);
    
    
    virtual void onTouchesBeganCallback(const std::vector<Touch*> &touches, Event *event);
};
 
#endif
cs



c++)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "test179.hpp"
 
USING_NS_CC;
 
Scene* Test179::createScene()
{
    auto scene = Scene::create();
    
    auto layer = Test179::create();
    scene->addChild(layer);
    
    return scene;
}
 
 
bool Test179::init()
{
    if ( !Layer::init())
    {
        return false;
    }
    
    
    //멀티터치 리스너 생성
    auto listener = EventListenerTouchAllAtOnce::create();
    
    //화면터치시 콜백메서드 설정
    /*
     onTouchBegan     : 이벤트 터치할때
     onTouchMoved     : 이벤트 이동시
     onTouchEnded     : 이벤트 뗄때
     onTouchCancelled : 이벤트 취소시
     */
    listener->onTouchesBegan = CC_CALLBACK_2(Test179::onTouchesBeganCallback, this);
    
    //이벤트 디스패처에 리스너 설정
    Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(listener, 1);
    
    
    return true;
}
 
 
//멀티터치시 호출되는 콜백메서드
void Test179::onTouchesBeganCallback(const std::vector<Touch*> &touches, Event *event){
    //여러개의 터치정보가 저장된 iterator 초기화
    std::vector<Touch*>::const_iterator iterator = touches.begin();
    
    Touch *touch;
    Point location[2];
    
    //터치한 횟수만큼 반복문
    for(int i=0; i<touches.size(); i++){
        //iterator를 통해 1개의 터치정보를 가져옴
        touch = (Touch*)(*iterator);
        
        location[i] = touch->getLocation(); //좌표정보 가져옴
        iterator++//다음 iterator
        
        CCLOG("location[%d] x = %f , y = %f", i, location[i].x, location[i].y);
    }
}
cs


+ Recent posts