cocos2d-x学习笔记——EventDispatcher事件分发机制

cocos2d-x学习笔记——EventDispatcher事件分发机制,第1张

概述EventDispatcher 事件分发机制先创建事件,注册到事件管理中心 _eventDispatcher,通过发布事件得到响应进行回调,完成事件流。_eventDispatcher是Node的属性,通过它管理当前节点(场景、层、精灵等)的所有事件的分发。但它本身是一个单例模式值的引用,在Node的构造函数中,通过Director::getInstance()->getEventDispatch

Eventdispatcher 事件分发机制先创建事件,注册到事件管理中心 _eventdispatcher,通过发布事件得到响应进行回调,完成事件流。_eventdispatcher是Node的属性,通过它管理当前节点(场景、层、精灵等)的所有事件的分发。但它本身是一个单例模式值的引用,在Node的构造函数中,通过Director::getInstance()->getEventdispatcher(); 获取,有了这个属性,就能方便的处理事件。

有五种不同的事件机制:

EventListenertouch 响应触控事件 EventListenerKeyboard 响应键盘事件 EventListeneracceleration 响应加速事件 EventListenMouse 响应鼠标事件 EventListenerCustom 响应自定义的事件

优先级:

addEventListenerWithFixedPriority(EventListener* Listener,int fixedPriority) 中 fixedPriority 的值越低,则优先级越高。

若优先级相同。Layer 的z顺序高的(绘制于顶部的)层将优于z顺序低的层。这将保证了诸如触碰事件的自顶向下传播

注意:如果是固定优先值的监听器添加到一个节点(addEventListenerWithFixedPriority),那当这个节点被移除时必须同时手动移除这个监听器,但是添加场景图优先监听器到节点(addEventListenerWithSceneGraPHPriority)就不用这么麻烦,监听器和节点是绑定好的,一旦节点的析构函数被调用,监听器也会同时被移除。

触摸事件:

voID EventdispatcherTest::funEventtouch(Sprite* sprite){    this->_eventdispatcher->removeAllEventListeners();    auto Listener = EventListenertouchOneByOne::create();    Listener->ontouchBegan = CC_CALLBACK_2(EventdispatcherTest::ontouchBeganss,this);    Listener->ontouchmoved = CC_CALLBACK_2(EventdispatcherTest::ontouchmovedss,this);    Listener->ontouchended = CC_CALLBACK_2(EventdispatcherTest::ontouchendedss,this);    Listener->ontouchCancelled = CC_CALLBACK_2(EventdispatcherTest::ontouchCancelledss,this);    Listener->setSwallowtouches(true);//是否向下传递    this->_eventdispatcher->addEventListenerWithSceneGraPHPriority(Listener,sprite);}bool EventdispatcherTest::ontouchBeganss(touch* touch,Event* ev){    auto target = static_cast<Sprite*>(ev->getCurrentTarget());    Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());    Size s = target->getContentSize();    Rect rect = Rect(0,0,s.wIDth,s.height);  //判断触摸区域是否在目标上    if (rect.containsPoint(locationInNode))    {        label->setString("ontouchBegan......");        target->setopacity(180);        return true;    }    return false;}voID EventdispatcherTest::ontouchmovedss(touch* touch,Event* ev){    auto target = static_cast<Sprite*>(ev->getCurrentTarget());    target->setposition(target->getposition() + touch->getDelta());    label->setString("ontouchmoved......");}voID EventdispatcherTest::ontouchendedss(touch* touch,Event* ev){    auto target = static_cast<Sprite*>(ev->getCurrentTarget());    target->setopacity(255);    label->setString("ontouchended......");}voID EventdispatcherTest::ontouchCancelledss(touch* touch,Event* ev){    label->setString("ontouchCancelled......");}

键盘事件:

voID EventdispatcherTest::funEventKeyboard(){    this->_eventdispatcher->removeAllEventListeners();    auto Listener = EventListenerKeyboard::create();    Listener->onKeypressed = CC_CALLBACK_2(EventdispatcherTest::onKeypressedss,this);    Listener->onkeyreleased = CC_CALLBACK_2(EventdispatcherTest::onkeyreleasedss,this);    this->_eventdispatcher->addEventListenerWithSceneGraPHPriority(Listener,this);}voID EventdispatcherTest::onKeypressedss(EventKeyboard::KeyCode keycode,Event* ev){    char txt[100] = {}; sprintf_s(txt,"key %d is pressed!",(int)keycode); label->setString(txt); } voID EventdispatcherTest::onkeyreleasedss(EventKeyboard::KeyCode keycode,Event* ev) { label->setString("key is Released!"); }

鼠标事件:

voID EventdispatcherTest::funEventMouse(Sprite* sprite){    this->_eventdispatcher->removeAllEventListeners();    auto Listener = EventListenerMouse::create();    Listener->onMouseDown = CC_CALLBACK_1(EventdispatcherTest::onMouseDownss,this);    Listener->onMouseMove = CC_CALLBACK_1(EventdispatcherTest::onMouseMovess,this);    Listener->onmouseup = CC_CALLBACK_1(EventdispatcherTest::onmouseupss,this);    Listener->onMouseScroll = CC_CALLBACK_1(EventdispatcherTest::onMouseScrollss,sprite);}voID EventdispatcherTest::onMouseDownss(Event* ev){    label->setString("onMouseDown!");}voID EventdispatcherTest::onMouseMovess(Event* ev){    label->setString("onMouseMove!");}voID EventdispatcherTest::onmouseupss(Event* ev){    label->setString("onmouseup!");}voID EventdispatcherTest::onMouseScrollss(Event* ev){    label->setString("onMouseScroll!");}

自定义事件:

//自定义事件funEventCustom();//2秒发派一次自定义事件,测试scheduleOnce(schedule_selector(EventdispatcherTest::dispatcherCustomEvents),2.0f);voID EventdispatcherTest::funEventCustom(){    auto Listener = EventListenerCustom::create("custom_event_1",CC_CALLBACK_1(EventdispatcherTest::onEventCustom,this));    this->_eventdispatcher->addEventListenerWithFixedPriority(Listener,1);//添加到事件分發器}voID EventdispatcherTest::dispatcherCustomEvents(float at){    //派發事件custom_event_1 事件內容為字符串custom event test!    this->_eventdispatcher->dispatchCustomEvent("custom_event_1","custom event test!");}voID EventdispatcherTest::onEventCustom(EventCustom* event){    auto data = static_cast<char*>(event->getUserData());    label->setString(data);}

加速事件:

 除了触摸,移动设备上一个很重要的输入源是设备的方向,因此大多数设备都配备了加速计,用于测量设备静止或匀速运动时所受到的重力方向。

重力感应来自移动设备的加速计,通常支持X,Y和Z三个方向的加速度感应,所以又称为三向加速计。在实际应用中,可以根据3个方向的力度大小来计算手机倾斜的角度或方向。

加速计监听器EventListeneracceleration,其静态create方法中有个acceleration的参数。acceleration是一个类,包含了加速计获得的3个方向的加速度。

voID EventdispatcherTest::funEventacceleration(){    //启动硬件设备    Device::setAccelerometerEnabled(true);     auto Listener = EventListeneracceleration::create(CC_CALLBACK_2(EventdispatcherTest::onacceleration,this));    this->_eventdispatcher->addEventListenerWithSceneGraPHPriority(Listener,this);}voID EventdispatcherTest::onacceleration(acceleration* acc,Event* event){    char str[100]={};    sprintf_s(str,"x:%2d,y:%2d,z:%2d,timestamp:%2d",acc->x,acc->y,acc->z,acc->timestamp);    log(str);}
总结

以上是内存溢出为你收集整理的cocos2d-x学习笔记——EventDispatcher事件分发机制全部内容,希望文章能够帮你解决cocos2d-x学习笔记——EventDispatcher事件分发机制所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1057565.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存