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

前端之家收集整理的这篇文章主要介绍了cocos2d-x学习笔记——EventDispatcher事件分发机制前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

有五种不同的事件机制:

  1. EventListenerTouch 响应触控事件
  2. EventListenerKeyboard 响应键盘事件
  3. EventListenerAcceleration 响应加速器事件
  4. EventListenMouse 响应鼠标事件
  5. EventListenerCustom 响应自定义的事件

优先级:

  1. addEventListenerWithFixedPriority(EventListener* listener,int fixedPriority) 中 fixedPriority 的值越低,则优先级越高。
  2. 若优先级相同。Layer 的z顺序高的(绘制于顶部的)层将优于z顺序低的层。这将保证了诸如触碰事件的自顶向下传播

  3. 注意:如果是固定优先值的监听器添加到一个节点(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相关文章