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