본문 바로가기

카테고리 없음

[cocos2d-x]싱글 터치 이벤트

 

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
#include "HelloWorldScene.h"
 
Scene* HelloWorld::createScene()
{
    auto scene = Scene::create();
 
    auto layer = HelloWorld::create();
    scene->addChild(layer);
 
    return scene;
}
 
bool HelloWorld::init()
{
    if (!Layer::init())
    {
        return false;
    }
 
    auto listener = EventListenerTouchOneByOne::create();
    //싱글 터치 이벤트 리스너를 생성하는 코드
    listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
    Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(listener, 1);
    //생성한 리스너를 이벤트 디스패처에 등록
    return true;
}
 
bool HelloWorld::onTouchBegan(Touch *touch, Event *unused_event)
{
    CCLOG("onTouchBegan");
    //터치를 했을때 메세지출력
    Point location = touch->getLocation();
 
    CCLOG("onTouchBegan : Location x = %f, y = %f", location.x, location.y);
    //터치한 좌표 출력
    return true;
}
cs


실행 결과