본문 바로가기

Cocos2d-x v3.17/고급 기능

[Cocos2d-x 고급기능]마우스 이벤트,커스텀 이벤트

반응형

마우스 이벤트 

   

위에서 계속 말했던것처럼 Cocos2d-x는 마우스 이벤트도 지원합니다. 

   

마우스 이벤트 리스너 생성: 

C++ 

_mouseListener = EventListenerMouse::create();

_mouseListener->onMouseMove = CC_CALLBACK_1(MouseTest::onMouseMove, this);

_mouseListener->onMouseUp = CC_CALLBACK_1(MouseTest::onMouseUp, this);

_mouseListener->onMouseDown = CC_CALLBACK_1(MouseTest::onMouseDown, this);

_mouseListener->onMouseScroll = CC_CALLBACK_1(MouseTest::onMouseScroll, this); 

_eventDispatcher->addEventListenerWithSceneGraphPriority(_mouseListener, this); 

void MouseTest::onMouseDown(Event *event)

{

// to illustrate the event....

EventMouse* e = (EventMouse*)event;

string str = "Mouse Down detected, Key: ";

str += tostr(e->getMouseButton());

} 

void MouseTest::onMouseUp(Event *event)

{

// to illustrate the event....

EventMouse* e = (EventMouse*)event;

string str = "Mouse Up detected, Key: ";

str += tostr(e->getMouseButton());

} 

void MouseTest::onMouseMove(Event *event)

{

// to illustrate the event....

EventMouse* e = (EventMouse*)event;

string str = "MousePosition X:";

str = str + tostr(e->getCursorX()) + " Y:" + tostr(e->getCursorY());

} 

void MouseTest::onMouseScroll(Event *event)

{

// to illustrate the event....

EventMouse* e = (EventMouse*)event;

string str = "Mouse Scroll detected, X: ";

str = str + tostr(e->getScrollX()) + " Y: " + tostr(e->getScrollY());

} 

   

출처: <http://cocos2d-x.org/docs/cocos2d-x/zh/event_dispatcher/mouse.html

   

커스텀 이벤트 


위에서 언급했던 이벤트는 전부 엔진에 내장된 이벤트입니다.

이것외에 당신은 커스텀 이벤트를 제작할수 있습니다.

이것은 엔진에서 발생하는 이벤트가 아니라 당신이 작성한 c++코드로 발생하는것입니다. 

커스텀 이벤트 리스너 생성: 

   

C++ 

_listener = EventListenerCustom::create("game_custom_event1", [=](EventCustom* event){

std::string str("Custom event 1 received, ");

char* buf = static_cast<char*>(event->getUserData());

str += buf;

str += " times";

statusLabel->setString(str.c_str());

}); 

_eventDispatcher->addEventListenerWithSceneGraphPriority(_listener, this); 

   

위에서 커스텀 이벤트 리스너를 구현하였고 응답방법을 설정했습니다.

아래는 커스텀이벤트를 생성하는것과 수동적으로 발생시키는 코드입니다. 

   

C++ 

static int count = 0;

++count; 

char* buf[10];

sprintf(buf, "%d", count); 

EventCustom event("game_custom_event1");

event.setUserData(buf); 

_eventDispatcher->dispatchEvent(&event); 

   

예시에선 커스텀 이벤트(EventCustom)개체를 사용하여 UserData를 설정하고 _eventDispatcher->dispatchEvent(&event)를 사용하여 수동적으로 이벤트를 발생시켰습니다.

우리가 정의한 이벤트 리스너가 이 이벤트를 전달받으면 대응하는 함수를 실행할것이고 함수는 이벤트 발생시 설정한 UserData를 사용하여 데이터 처리를 완성할겁니다. 

주의:EventCustom 과 EventListenerCustom의 첫번째 매개변수는 "game_custom_event1"이라는 문자열입니다. 

   

출처: <http://cocos2d-x.org/docs/cocos2d-x/zh/event_dispatcher/custom.html

 

 

조금더 나아가서... 

이벤트 리스너 등록 

우리가 여러객체가 동일한 이벤트에 응답할시에는 하나의 이벤트 리스너를 생성하고 eventDispatcher를 통하여 여러 개체를 등록할수 있습니다. 


C++ 

// Add listener

_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1,

sprite1); 


주의해야 할점은 여러개체를 추가시킬때 clone()메서드를 사용해야 합니다. 

C++ 

// Add listener

_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1,

sprite1); 

// Add the same listener to multiple objects.

_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(),

sprite2); 

_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(),

sprite3); 


이벤트 삭제 

아래의 방법으로 이미 추가한 이벤트 리스너를 삭제할수 있습니다. 

C++ 

_eventDispatcher->removeEventListener(listener); 

내장노드 개체의 이벤트 발생 매커니즘은 위에서 말한것과 일치합니다.

예를들면 당신이 메뉴에 있는 항목을 클릭하였을때도 똑같은 이벤트가 발생합니다.

즉 당신이 내장된 객체에 removeEventListener()를 사용하여 이벤트를 발생하지 않게 만들수 있다는 말입니다. 

   

출처: <http://cocos2d-x.org/docs/cocos2d-x/zh/event_dispatcher/registering.html

 


반응형