移动设备的最主要的操作方式就是触摸输入,对于游戏而言更是如此。触摸事件和鼠标事件有一些很大的区别,比如鼠标可以悬停,触摸可以多点,鼠标有右键,触摸可以使用手势。3.0版对触摸响应做了修改,增加了事件监听,修改了事件分发。可能是太新了,还没有找到相关的学习资料。暂时先翻译官网的release notes吧。
新的事件分发器
像touch,keyboard,acceleration,custom事件,所有的事件都用EventDispatcher做分发。去掉了TouchDispatcher,KeyboardDispatcher,KeypadDispatcher,AccelerometerDispatcher。
加入Touch事件监听
单点触控
auto sprite = Sprite::create("file.png");
...
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouch(true);
listener->onTouchBegan = [](Touch* touch, Event* event) { do_some_thing(); return true; };
listener->onTouchMoved = [](Touch* touch, Event* event) { do_some_thing(); };
listener->onTouchEnded = [](Touch* touch, Event* event) { do_some_thing(); };
listener->onTouchCancelled = [](Touch* touch, Event* event) { do_some_thing(); };
// The priority of the touch listener is based on the draw order of sprite
EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, sprite);
// Or the priority of the touch listener is a fixed value
EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, 100); // 100 is a fixed value
多点触控
auto sprite = Sprite::create("file.png");
...
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesBegan = [](const std::vector<Touch*>& touches, Event* event) { do_some_thing(); };
listener->onTouchesMoved = [](const std::vector<Touch*>& touches, Event* event) { do_some_thing(); };
listener->onTouchesEnded = [](const std::vector<Touch*>& touches, Event* event) { do_some_thing(); };
listener->onTouchesCancelled = [](const std::vector<Touch*>& touches, Event* event) { do_some_thing(); };
// The priority of the touch listener is based on the draw order of sprite
EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, sprite);
// Or the priority of the touch listener is a fixed value
EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, 100); // 100 is a fixed value
加入Mouse事件监听
auto mouseListener = EventListenerMouse::create();
mouseListener->onMouseScroll = [](Event* event) { EventMouse* e = static_cast<EventMouse*>(event); do_some_thing(); };
mouseListener->onMouseUp = [](Event* event) { EventMouse* e = static_cast<EventMouse*>(event); do_some_thing(); };
mouseListener->onMouseDown = [](Event* event) { EventMouse* e = static_cast<EventMouse*>(event); do_some_thing(); };
dispatcher->addEventListenerWithSceneGraphPriority(mouseListener, this);
加入Keyboard事件监听
auto listener = EventListenerKeyboard::create();
listener->onKeyPressed = CC_CALLBACK_2(SomeClass::onKeyPressed, this);
listener->onKeyReleased = CC_CALLBACK_2(SomeClass::onKeyReleased, this);
EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this);
加入Acceleration事件监听
auto listener = EventListenerAcceleration::create(CC_CALLBACK_2(SomeClass::onAcceleration, this));
EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this);
加入自定义事件监听
auto listener = EventListenerCustom::create("game_custom_event", [=](EventCustom* event){
void* userData= event->getUserData();
do_some_with_user_data();
});
dispatcher->addEventListenerWithFixedPriority(listener, 1);
分发一个自定义事件
EventCustom event("game_custom_event");
event.setUserData(some_data);
dispatcher->dispatchEvent(&event);
为监听设定一个优先级
dispatcher->setPriority(fixedPriorityListener, 200);
移除事件监听
移除一个指定的监听
dispatcher->removeEventListener(listener);
移除自定义监听
dispatcher->removeCustomEventListener("my_custom_event_listener_name");
移除某种事件监听类型的所有监听
dispatcher->removeEventListeners(EventListener::Type::TOUCH_ONE_BY_ONE);
移除所有监听
dispatcher->removeAllListeners();