zoukankan      html  css  js  c++  java
  • [cocos2d-x]关于3.x的触摸机制

    触摸机制的概念

    通过对要监听触摸的节点进行注册,然后自定义相应的触摸函数,由事件监听器实现对触摸的监听并且实现相应的响应动作。

    触摸的分类

    这里写图片描述

    单点触摸
    下面是实现单点触摸监听的步骤:

    //第一步:先创建一个要进行触摸检测的精灵
    auto spr=Sprite::create("helloworld.png");
    //第二步:创建一个事件监听器
    auto touchListener= EventListenerTouchOneByOne::create();
    //第三步:设置不让触摸检测传递到下一层
    touchListener->setSwallowTouches(true);
    
    //******重点*****
    //第四步(第一种方法):设置相应的触摸函数
    //先在头文件中声明四个回调函数:
    std::function<bool(Touch*t, Event*)>onTouchBegan;
    std::function<void(Touch*, Event*)>onTouchMoved;  
    std::function<void(Touch*, Event*)>onTouchEnded;  
    std::function<void(Touch*, Event*)> onTouchCancelled;
    //然后在设置对应函数:
    listner>onTouchBegan=CC_CALLBACK_2(GameTest::onTouchBegan,this);
    listner->onTouchMoved=CC_CALLBACK_2(GameTest::onTouchMoved, this);
    listner->onTouchEnded=CC_CALLBACK_2(GameTest::onTouchEnded, this);
    //将监听器添加带事件派发器中
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listner, spr);
    //给监听器设置优先级,越高越先响应
    listner->setFixedPriority(1);
    
    //第四步(第二种方法):使用lambda表达式,这种更加方便
    listner->onTouchBegan = [](Touch*t, Event*e) {  return true; };
    listner->onTouchMoved = [](Touch*t, Event*e) {};
    listner->onTouchEnded = [](Touch*t, Event*e) {};

    多点触摸
    多点触摸和单点触摸类似,不过使用的参数使用vector数组来存储了多个touch触摸点的坐标位置,下面是不同的地方

    std::function<void(conststd::vector<Touch*>&,Event*)> onTouchesBegan;  
    
    std::function<void(conststd::vector<Touch*>&,Event*)> onTouchesMoved;  
    
    std::function<void(conststd::vector<Touch*>&,Event*)> onTouchesEnded;  
    
    std::function<void(conststd::vector<Touch*>&,Event*)> onTouchesCancelled;

    触摸函数的两个参数的含义

    Touch*的作用:传入触摸点的坐标位置,下面是Touch类中提供的一些方法。

        Vec2 getLocation() const;
        /** Returns the previous touch location in OpenGL coordinates.
         *
         * @return The previous touch location in OpenGL coordinates.
         */
        Vec2 getPreviousLocation() const;
        /** Returns the start touch location in OpenGL coordinates.
         *
         * @return The start touch location in OpenGL coordinates.
         */
        Vec2 getStartLocation() const;
        /** Returns the delta of 2 current touches locations in screen coordinates.
         *
         * @return The delta of 2 current touches locations in screen coordinates.
         */
        Vec2 getDelta() const;
        /** Returns the current touch location in screen coordinates.
         *
         * @return The current touch location in screen coordinates.
         */
        Vec2 getLocationInView() const;
        /** Returns the previous touch location in screen coordinates. 
         *
         * @return The previous touch location in screen coordinates.
         */
        Vec2 getPreviousLocationInView() const;
        /** Returns the start touch location in screen coordinates.
         *
         * @return The start touch location in screen coordinates.
         */
        Vec2 getStartLocationInView() const;
    
        /** Set the touch information. It always used to monitor touch event.
         *
         * @param id A given id
         * @param x A given x coordinate.
         * @param y A given y coordinate.
         */

    Event*作用:主要是传入要进行操作的对象比如:键盘,鼠标,游戏手柄等等,下面是它提供的一些函数。

        Type getType() const { return _type; }
    
        /** Stops propagation for current event.
         */
        void stopPropagation() { _isStopped = true; }
    
        /** Checks whether the event has been stopped.
         *
         * @return True if the event has been stopped.
         */
        bool isStopped() const { return _isStopped; }
    
        /** Gets current target of the event.
         * @return The target with which the event associates.
         * @note It's only available when the event listener is associated with node.
         *        It returns 0 when the listener is associated with fixed priority.
         */
        Node* getCurrentTarget() { return _currentTarget; }

    其他注意事项

    virtual bool onTouchBegan( Touch *Touch, Event *Event);

    ①如果返回false,则本层的onTouchMoved(),onTouchEnded()不会再接收到消息,但是本层之下的其它层会接收到消息

    ②如果返回true,则本层的onTouchMoved(),onTouchEnded()可以接收到消息,但是本层之下的其它层不能再接收到消息

    https://github.com/li-zheng-hao
  • 相关阅读:
    VysorPro助手
    Play 2D games on Pixel running Android Nougat (N7.1.2) with Daydream View VR headset
    Play 2D games on Nexus 6P running Android N7.1.1 with Daydream View VR headset
    Native SBS for Android
    ADB和Fastboot最新版的谷歌官方下载链接
    How do I install Daydream on my phone?
    Daydream Controller手柄数据的解析
    蓝牙BLE传输性能及延迟分析
    VR(虚拟现实)开发资源汇总
    Android(Java)控制GPIO的方法及耗时分析
  • 原文地址:https://www.cnblogs.com/lizhenghao126/p/11053723.html
Copyright © 2011-2022 走看看