cocos2d-x事件分发机制

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

监听器

  • EventListenerTouch:响应触摸事件
  • EventListenerKeyboard:响应键盘事件
  • EventListnerAcceleration:响应加速度事件
  • EventListenMouse:响应鼠标事件
  • EventListnerCustom:响应自定义事件
// 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;
};

优先级

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

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

触摸事件

//  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);

onTouchBegan开始触摸屏幕时

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

onTouchEnded结束触摸屏幕时

键盘事件

// creating a keyboard event listener
auto listener = EventListenerKeyboard::create();
listener->onKeyPressed = CC_CALLBACK_2(KeyboardTest::onKeyPressed,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按键被按下时

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

加速度传感器事件

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 callback function prototype
void AccelerometerTest::onAcceleration(Acceleration* acc,Event* event)
{
    //  Processing logic here
}

鼠标事件

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

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