COCOS2D-X event distribution mechanism

Listener

  • EventListenerTouch: Responding to touch events
  • EventListenerKeyboard: Responding to keyboard events
  • EventListnerAcceleration: respond to acceleration events
  • EventListenMouse: respond to mouse events
  • EventListnerCustom: respond to custom events
// When "swallow touches" is true, then returning'true' from the
// onTouchBegan method will "swallow" the touch event, preventing
// other listeners from using it.
listener1- >setSwallowTouches(true);

// you should also return true in onTouchBegan()

listener1->onTouchBegan = [](Touch* touch, Event* event){
// your code

return true;
};

Priority

Fixed value priority: use an integer With a numerical value, a listener with a lower data value will receive an event first than a listener with a higher value.

Scene priority: It is the pointer to the node object. The listener in the node with higher z-order will receive the event first than the node with higher z-order.

Touch events

// Create a "one by one" touch event listener
// (processes one touch at a time)
auto listener1 = EventListenerTouchOneByOne ::create();

// trigger when you push down
listener1->onTouchBegan = [](Touch* touch, Event* event){
// your code
return true; // if you are consuming it
};

// trigger when moving touch
listener1->onTouchMoved = [](Touch* touch, Event* event){
// your code
};

// trigger when you let up
listener1->onTouchEnded = [=](Touch* touch , Event* event){
// your code
};

// Add listener
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, this);

When onTouchBegan starts to touch the screen

When onTouchMoved touches the screen and moves on the screen at the same time

When onTouchEnded finishes touching the screen

Keyboard events

// creating a keyboard event listener
auto listener = EventListenerKeyboard::create();
listener->onKeyPressed = CC_CALLBACK_2(KeyboardTest::onKe yPressed, this);
listener->onKeyReleased = CC_CALLBACK_2(KeyboardTest::onKeyReleased, this);

_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

// Implementation of the keyboard event callback function prototype
void KeyboardTest::onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event)
{
log("Key with keycode %d pressed", keyCode);
}

void KeyboardTest::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event)
{
log("Key with keycode %d released ", keyCode);
}

onKeyPressed when the button is pressed

onKeyReleased when the pressed button is released

acceleration sensor event

Device::setAccelerometerEnabled(true);
// creating an accelerometer event
auto listener = EventListenerAcceleration::create(CC_CALLBACK_2(
AccelerometerTest::onAcceleration , this));

_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

// Implementation of the acce lerometer callback function prototype
void AccelerometerTest::onAcceleration(Acceleration* acc, Event* event)
{
// Processing logic here
}

Mouse event

_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());
}< br />
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;< br /> 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());
}

Listener

  • EventListenerTouch: Respond to touch events
  • EventListenerKeyboard: respond to keyboard events
  • EventListnerAcceleration: respond to acceleration events
  • EventListenMouse: respond to mouse events
  • EventListnerCustom: respond to custom events
// When "swallow touches" is true, then returning'true' from the
// onTouchBegan method will "swallow" the touch event, preventing
// other listeners from using it.
listener1->setSwallowTouches(true);

// you should also return true in onTouchBegan()

listener1->onTouchBegan = [](Touch* touch, Event* event){
// your code
< br /> return true;
};

Priority

Fixed value priority: use an integer value, the listener with lower data is better than the listener with higher value The listener receives the event first.

Scene priority: It is the pointer to the node object. The listener in the node with higher z-order will receive the event first than the node with higher z-order.

Touch events

// Create a "one by one" touch event listener
// (processes one touch at a time)
auto listener1 = EventListenerTouchOneByOne ::create();

// trigger when you push down
listener1->onTouchBegan = [](Touch* touch, Event* event){
// your code
return true; // if you are consuming it
};

// trigger when moving touch
listener1->onTouchMoved = [](Touch* touch, Event* event){
// your code
};

// trigger when you let up
listener1->onTouchEnded = [=](Touch* touch , Event* event){
// your code
};

// Add listener
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, this);

When onTouchBegan starts to touch the screen

When onTouchMoved touches the screen and moves on the screen at the same time

When onTouchEnded finishes touching the screen

Keyboard events

// creating a keyboard event listener
auto listener = EventListenerKeyboard::create();
listener->onKeyPressed = CC_CALLBACK_2(KeyboardTest::onKeyPresse d, this);
listener->onKeyReleased = CC_CALLBACK_2(KeyboardTest::onKeyReleased, this);

_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

// Implementation of the keyboard event callback function prototype
void KeyboardTest::onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event)
{
log("Key with keycode %d pressed", keyCode);
}

void KeyboardTest::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event)
{
log("Key with keycode %d released ", keyCode);
}

onKeyPressed when the button is pressed

onKeyReleased when the pressed button is released

acceleration sensor event

Device::setAccelerometerEnabled(true);
// creating an accelerometer event
auto listener = EventListenerAcceleration::create(CC_CALLBACK_2(
AccelerometerTest::onAcceleration , this));

_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

// Implementation of the accelerometer ca llback function prototype
void AccelerometerTest::onAcceleration(Acceleration* acc, Event* event)
{
// Processing logic here
}

Mouse event< /h3>

_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());
}< br />
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());< br />}

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3667 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.