什么是事件? Event及子类EventXXX,核心成员_type
谁监听事件? EventListener及子类EventListenerXXX,核心成员_onEvent
谁派发事件? EventDispatcher分发器,核心方法dispatchEvent,(导演类全局实例化了一个EventDispatcher)
基本概念:
事件分发本质是采用的观察者模式: 注册观察者,事件产生并回调,取消注册
在手游中,最重要的事件是触摸事件。它们很容易被创建来提供通用的功能。首先我们要明确什么是触摸事件。当你触摸移动设备的屏幕时,屏幕会接收到这个触摸行为,并检查你触摸了哪里以及决定你触摸到了什么。然后你的触摸行为就会被响应。你所触摸的或许不是响应对象,但很有可能是它下面的东西。通常会给触摸事件分配优先级,优先级最高的就是被先响应的。以下代码创建了一个基本的触摸事件监听器:
// 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);
//Initializing and binding
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);
}
_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)
{
EventMouse* e = (EventMouse*)event;
string str = "Mouse Down detected, Key: ";
str += tostr(e->getMouseButton());
// ...
}
void MouseTest::onMouseUp(Event *event)
{
EventMouse* e = (EventMouse*)event;
string str = "Mouse Up detected, Key: ";
str += tostr(e->getMouseButton());
// ...
}
void MouseTest::onMouseMove(Event *event)
{
EventMouse* e = (EventMouse*)event;
string str = "MousePosition X:";
str = str + tostr(e->getCursorX()) + " Y:" + tostr(e->getCursorY());
// ...
}
void MouseTest::onMouseScroll(Event *event)
{
EventMouse* e = (EventMouse*)event;
string str = "Mouse Scroll detected, X: ";
str = str + tostr(e->getScrollX()) + " Y: " + tostr(e->getScrollY());
// ...
}
很些移动设备都配备了加速度计。加速计是一个传感器,可以测量重力和方向上的变化。例如,来回移动你的电话来模拟平衡。Cocos2d-x也支持这些事件并且创建起来很简单。在使用加速计事件之前,你需要在设备上激活这个事件:
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
}
上面的事件类型都是系统定义的,所以事件由系统自动触发。作为额外的,你可以自定义不由系统触发的事件,代码类似下面:
_listener = EventListenerCustom::create("game_custom_event1", [=](EventCustom* event){
std::string str("Custom event 1 received, ");
char* buf = static_cast(event->getUserData());
str += buf;
str += " times";
statusLabel->setString(str.c_str());
});
_eventDispatcher->addEventListenerWithFixedPriority(_listener, 1);
自定义的事件监听器正如上面所示,提供一个响应函数并注册到事件分发器。不过你还需要通过下面的代码来实现事件的触发:
static int count = 0;
++count;
char* buf = new char[10];
sprintf(buf, "%d", count);
EventCustom event("game_custom_event1");
event.setUserData(buf);
_eventDispatcher->dispatchEvent(&event);
CC_SAFE_DELETE_ARRAY(buf);
上面的例子创建了一个EventCustom对象并且设置了UserData,然后调用代码_eventDispatcher->dispatchEvent(&event);手工地分发事件。这样就能触发前面定义的回调函数。
使用事件分配器可以很容易的注册事件。以上文的触摸事件监视器为例:
// Add listener
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1,sprite1);
值得注意的是,每个对象都只能注册一个触摸事件。如果多个对象需要使用相同的监听器,你需要使用clone()方法.
// Add listener
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1,sprite1);
// Add the same listener to multiple objects.
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite2);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite3);
使用如下方法可以移除一个已有的监听器:
_eventDispatcher->removeEventListener(listener);
尽管这看起来很特殊,但是内置的Node对象使用事件分发机制与我们所讲的方式是相同的。以Menu为例,当点击带有MenuItems属性的 Menu时,你就已经分配到了一个事件。同样也可对内置的Node对象使用removeEventListener方法。
registerScriptTouchHandler 详解(可以设置单点或多点)
function gameWindow:addLayerTouchEventMethod1()
local function onTouchEvent(eventType, x, y)
--log("eventType = "..tostring(eventType))
if eventType == "began" then
--需要返回true
return onTouchBegan(touch, event)
elseif eventType == "moved" then
onTouchMoved(touch, event)
elseif eventType == "ended" then
onTouchEnded(touch, event)
end
end
config.bottomLayer:setTouchEnabled(true)
config.bottomLayer:registerScriptTouchHandler(onTouchEvent)
end
registerScriptTapHandler 注册点击事件
function gameWindow:addBtn()
local btn = cc.MenuItemImage:create("white.png", "black.png", "black.png")
btn:setPosition(320, 160)
local function btnClick()
log("btnClick")
end
btn:registerScriptTapHandler(btnClick)
local menu = cc.Menu:create()
config.bottomLayer:addChild(menu)
menu:setPosition(cc.p(0,0))
menu:addChild(btn)
end
registerScriptHandler 注册基本事件
function gameWindow:addLayerTouchEventMethod2()
--创建一个单点触屏事件
local listener = cc.EventListenerTouchOneByOne:create()
--注册触屏开始事件
listener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN)
--注册触屏移动事件
listener:registerScriptHandler(onTouchMoved, cc.Handler.EVENT_TOUCH_MOVED)
--注册触屏结束事件
listener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED)
--获取层的事件派发器
local eventDispatcher = config.bottomLayer:getEventDispatcher()
--事件派发器 注册一个node事件
eventDispatcher:addEventListenerWithSceneGraphPriority(listener, config.bottomLayer)
end
注册layer的进入/退出事件用法
function gameWindow:addLayerEnterAndExitEvent()
local function onNodeEvent(eventType)
if eventType == "enter" then
log("enter")
elseif eventType == "exit" then
log("exit")
end
end
config.bottomLayer:registerScriptHandler(onNodeEvent)
end
事件分发机制