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

相关文章

操作步骤 1、创建cocos2d-x工程 2、新建 Scene1.cpp Scene1.h Scene1.h代码 #ifndef __SCENE1_H__#defi...
开发环境:OS(WINDOWS 8.1 X64 企业版) cocos2d-x 2.2.1 vs2010 想给vs安装上cocos的模版,执行Install...
把创建项目做成一个批处理,当创建项目时可以省时省力很多。 操作步骤 1、在 E:cocos2d-x-2.2.1toolspr...
https://www.cnblogs.com/JiaoQing/p/3906780.html 四个响应函数 1 EventListenerPhysicsContact* evC...
转载于 http://www.cnblogs.com/kenkofox/p/3926797.html 熟悉js的dom事件或者flash事件的,基本都能立...
ScrollView(滚动容器)加载大量item时会导致游戏界面的卡顿,严重时整个界面会出现卡死的情况。最近项...