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


  • 相关阅读:
    Java 简单算法--打印乘法口诀(只使用一次循环)
    Java简单算法--求100以内素数
    ubuntu 16.04 chrome flash player 过期
    java 网络API访问 web 站点
    java scoket (UDP通信模型)简易聊天室
    leetcode1105 Filling Bookcase Shelves
    leetcode1140 Stone Game II
    leetcode1186 Maximum Subarray Sum with One Deletion
    leetcode31 Next Permutation
    leetcode834 Sum of Distances in Tree
  • 原文地址:https://www.cnblogs.com/lxjshuju/p/6736003.html
Copyright © 2011-2022 走看看