鼠标事件

就像前几节介绍的那样,Cocos2d-x 支持响应鼠标事件

创建鼠标事件监听器:

  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. _eventDispatcher->addEventListenerWithSceneGraphPriority(_mouseListener, this);
  7. void MouseTest::onMouseDown(Event *event)
  8. {
  9. // to illustrate the event....
  10. EventMouse* e = (EventMouse*)event;
  11. string str = "Mouse Down detected, Key: ";
  12. str += tostr(e->getMouseButton());
  13. }
  14. void MouseTest::onMouseUp(Event *event)
  15. {
  16. // to illustrate the event....
  17. EventMouse* e = (EventMouse*)event;
  18. string str = "Mouse Up detected, Key: ";
  19. str += tostr(e->getMouseButton());
  20. }
  21. void MouseTest::onMouseMove(Event *event)
  22. {
  23. // to illustrate the event....
  24. EventMouse* e = (EventMouse*)event;
  25. string str = "MousePosition X:";
  26. str = str + tostr(e->getCursorX()) + " Y:" + tostr(e->getCursorY());
  27. }
  28. void MouseTest::onMouseScroll(Event *event)
  29. {
  30. // to illustrate the event....
  31. EventMouse* e = (EventMouse*)event;
  32. string str = "Mouse Scroll detected, X: ";
  33. str = str + tostr(e->getScrollX()) + " Y: " + tostr(e->getScrollY());
  34. }