cocos2d-x事件分发机制

前端之家收集整理的这篇文章主要介绍了cocos2d-x事件分发机制前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

监听器

  • EventListenerTouch:响应触摸事件
  • EventListenerKeyboard:响应键盘事件
  • EventListnerAcceleration:响应加速度事件
  • EventListenMouse:响应鼠标事件
  • EventListnerCustom:响应自定义事件
  1. // When "swallow touches" is true,then returning 'true' from the
  2. // onTouchBegan method will "swallow" the touch event,preventing
  3. // other listeners from using it.
  4. listener1->setSwallowTouches(true);
  5.  
  6. // you should also return true in onTouchBegan()
  7.  
  8. listener1->onTouchBegan = [](Touch* touch,Event* event){
  9. // your code
  10.  
  11. return true;
  12. };

优先级

固定值优先级:使用一个整形的数值,数据较低的监听器比数值较高的监听器,先接收到事件。

场景优先级:是指向节点对象的指针,z-order较高的节点中的监听器比z-order较你的节点中,先接收到事件。

触摸事件

  1. // Create a "one by one" touch event listener
  2. // (processes one touch at a time)
  3. auto listener1 = EventListenerTouchOneByOne::create();
  4.  
  5. // trigger when you push down
  6. listener1->onTouchBegan = [](Touch* touch,Event* event){
  7. // your code
  8. return true; // if you are consuming it
  9. };
  10.  
  11. // trigger when moving touch
  12. listener1->onTouchMoved = [](Touch* touch,Event* event){
  13. // your code
  14. };
  15.  
  16. // trigger when you let up
  17. listener1->onTouchEnded = [=](Touch* touch,Event* event){
  18. // your code
  19. };
  20.  
  21. // Add listener
  22. _eventDispatcher->addEventListenerWithSceneGraPHPriority(listener1,this);

onTouchBegan开始触摸屏幕时

onTouchMoved触摸屏幕,同时在屏幕上移动时

onTouchEnded结束触摸屏幕时

键盘事件

  1. // creating a keyboard event listener
  2. auto listener = EventListenerKeyboard::create();
  3. listener->onKeyPressed = CC_CALLBACK_2(KeyboardTest::onKeyPressed,this);
  4. listener->onKeyReleased = CC_CALLBACK_2(KeyboardTest::onKeyReleased,this);
  5.  
  6. _eventDispatcher->addEventListenerWithSceneGraPHPriority(listener,this);
  7.  
  8. // Implementation of the keyboard event callback function prototype
  9. void KeyboardTest::onKeyPressed(EventKeyboard::KeyCode keyCode,Event* event)
  10. {
  11. log("Key with keycode %d pressed",keyCode);
  12. }
  13.  
  14. void KeyboardTest::onKeyReleased(EventKeyboard::KeyCode keyCode,Event* event)
  15. {
  16. log("Key with keycode %d released",keyCode);
  17. }

onKeyPressed按键被按下时

onKeyReleased按下状态的按键被放开时

加速度传感器事件

  1. Device::setAccelerometerEnabled(true);
  1. // creating an accelerometer event
  2. auto listener = EventListenerAcceleration::create(CC_CALLBACK_2(
  3. AccelerometerTest::onAcceleration,this));
  4.  
  5. _eventDispatcher->addEventListenerWithSceneGraPHPriority(listener,this);
  6.  
  7. // Implementation of the accelerometer callback function prototype
  8. void AccelerometerTest::onAcceleration(Acceleration* acc,Event* event)
  9. {
  10. // Processing logic here
  11. }

鼠标事件

  1. _mouseListener = EventListenerMouse::create();
  2. _mouseListener->onMouseMove = CC_CALLBACK_1(MouseTest::onMouseMove,this);
  3. _mouseListener->onMouseUp = CC_CALLBACK_1(MouseTest::onMouseUp,this);
  4. _mouseListener->onMouseDown = CC_CALLBACK_1(MouseTest::onMouseDown,this);
  5. _mouseListener->onMouseScroll = CC_CALLBACK_1(MouseTest::onMouseScroll,this);
  6.  
  7. _eventDispatcher->addEventListenerWithSceneGraPHPriority(_mouseListener,this);
  8.  
  9. void MouseTest::onMouseDown(Event *event)
  10. {
  11. // to illustrate the event....
  12. EventMouse* e = (EventMouse*)event;
  13. string str = "Mouse Down detected,Key: ";
  14. str += tostr(e->getMouseButton());
  15. }
  16.  
  17. void MouseTest::onMouseUp(Event *event)
  18. {
  19. // to illustrate the event....
  20. EventMouse* e = (EventMouse*)event;
  21. string str = "Mouse Up detected,Key: ";
  22. str += tostr(e->getMouseButton());
  23. }
  24.  
  25. void MouseTest::onMouseMove(Event *event)
  26. {
  27. // to illustrate the event....
  28. EventMouse* e = (EventMouse*)event;
  29. string str = "MousePosition X:";
  30. str = str + tostr(e->getCursorX()) + " Y:" + tostr(e->getCursorY());
  31. }
  32.  
  33. void MouseTest::onMouseScroll(Event *event)
  34. {
  35. // to illustrate the event....
  36. EventMouse* e = (EventMouse*)event;
  37. string str = "Mouse Scroll detected,X: ";
  38. str = str + tostr(e->getScrollX()) + " Y: " + tostr(e->getScrollY());
  39. }

猜你在找的Cocos2d-x相关文章