zoukankan      html  css  js  c++  java
  • Cocos2d-x3.0触摸


    cocos2d-x 3.0開始採用C++11,并開始脱离OC风格,在触摸处理上也作出了改变

    C++11带来了Lambda表达式(匿名函数),它将简化程序编写,可使代码更清晰易懂

    在旧2.x版本号的触摸机制之中,是须要重写相关虚函数

    在新的版本号中,触摸机制进行了改动,你能够随时改动事件对象,还能够使用Lammbda表达式(匿名函数),还能够移除监听器等


    --------------------------------------------------------

    事件监听器:封装您的事件处理代码。

       
    事件调度程序:通知用户事件的监听器。        
    事件对象:包括关于事件的信息。

                  

    --------------------------------------------------------


    创建监听器

        ///创建单击事件监听者   ( EventListenerTouchAllAtOnce  则是多点)
        auto touchListener=EventListenerTouchOneByOne::create();
        ///setSwallowTouches  使当前层的touch事件不会传递给它之下的层
        touchListener->setSwallowTouches(true);


    事件对象绑定

    ///Lambda表达式  PS:这里的[=] 代表 截取外部作用域中全部变量,并拷贝一份在函数体中使用,详情能够翻阅其它资料
        touchListener->onTouchBegan=[=](Touch* touch, Event* evnet)
        {
            cocos2d::Point  s_PLocation=touch->getLocation();
            addSpriteAtArcRandom(s_PLocation);
            return true;
        };
    
        ///C++11 bind模板函数(事实上也就是CC_CALLBACK_XX宏)
        touchListener->onTouchEnded=std::bind(&HelloWorld::onTouchEnded, this,std::placeholders::_1,std::placeholders::_2);
        touchListener->onTouchMoved=std::bind(&HelloWorld::onTouchMoved, this,std::placeholders::_1,std::placeholders::_2);


    将事件监听器增加调度器中


        auto dispatcher=Director::getInstance()->getEventDispatcher();
        ///加入事件监听器,并设置优先级,PS:既然有加入,当然有移除(removeEventListeners(指定对象),removeEventListenersForType(指定类型)等),嘿嘿,  比曾经方便了不少
        dispatcher->addEventListenerWithFixedPriority(touchListener, 1);

    (1)addEventListenerWithSceneGraphPriority:注冊监听事件,监听优先级按绘制顺序来

    (2)addEventListenerWithFixedPriority:注冊监听事件,能够设置事件的监听的优先级别

    (3)addCustomEventListener:注冊自己定义监听事件,而且监听优先级固定为1


    可參考:  http://www.cocos2d-x.org/docs/manual/framework/native/input/event-dispatcher/en


  • 相关阅读:
    SICP学习笔记(1.3.4)
    SICP学习笔记(1.1.7 ~ 1.1.8)
    It's Not Just Standing Up: Patterns of Daily Standup Meetings
    SICP学习笔记(1.1.4~1.1.5)
    SICP学习笔记(2.1.4)
    呵呵呵,传说中的吉普赛读心术(WPF版)
    利用SendMessage实现窗口拖动
    SICP学习笔记(2.2.2)
    SICP学习笔记(2.3.1~2.3.2)
    SICP学习笔记(2.3.3)
  • 原文地址:https://www.cnblogs.com/lxjshuju/p/6736003.html
Copyright © 2011-2022 走看看