싱글터치 이벤트 설정하기



hpp)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef test176_hpp
#define test176_hpp
 
#include "cocos2d.h"
 
USING_NS_CC;
 
class Test176 : public cocos2d::Layer
{
public:
    static cocos2d::Scene* createScene();
    
    virtual bool init();
    
    // implement the "static create()" method manually
    CREATE_FUNC(Test176);
    
    
    virtual bool onTouchBegan(Touch *touch, Event *unused_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
#include "test176.hpp"
 
USING_NS_CC;
 
Scene* Test176::createScene()
{
    auto scene = Scene::create();
    
    auto layer = Test176::create();
    scene->addChild(layer);
    
    return scene;
}
 
 
bool Test176::init()
{
    if ( !Layer::init())
    {
        return false;
    }
 
    
    //싱글터치 리스너 생성
    auto listener = EventListenerTouchOneByOne::create();
    
    //화면 터치시 콜백메서드 지정
    /*
        onTouchBegan     : 이벤트 터치할때
        onTouchMoved     : 이벤트 이동시
        onTouchEnded     : 이벤트 뗄때
        onTouchCancelled : 이벤트 취소시
    */
    listener->onTouchBegan = CC_CALLBACK_2(Test176::onTouchBegan, this);
    
    //이벤트 디스패처에 리스너 추가
    Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(listener, 1);
    
    
    return true;
}
 
 
//터치시 호출되는 콜백함수 설정
bool Test176::onTouchBegan(Touch *touch, Event *event){
    Point point = touch->getLocation();
    
    CCLOG("onTouchBegan Location x = %f , y = %f", point.x, point.y);
    
    return true;
}
cs



결과

1
2
3
4
5
6
onTouchBegan Location x = 223.969864 , y = 197.965469
onTouchBegan Location x = 109.229088 , y = 178.560791
onTouchBegan Location x = 299.901245 , y = 134.689316
onTouchBegan Location x = 366.552155 , y = 206.402298
onTouchBegan Location x = 47.640285 , y = 219.057526
onTouchBegan Location x = 100.792252 , y = 102.629379
cs


+ Recent posts