• 5.触摸touch,单点触摸,多点触摸,触摸优先和触摸事件的吞噬


    

    1 触摸

    Coco2dx默认仅仅有CCLayer及其派生类才有触摸的功能。

    2 单点触摸

    打开触摸开关和触摸方式

    setTouchEnabled(true);

    setTouchMode(kCCTouchesOneByOne);

    Cocos2dx 对触摸分三布来处理。分是是点触。移动。离开。

    或是中间被打断。

    其功能皆有对应的virtual 函数进行override 的。

    virtual bool ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent);

    virtual void ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent);

    virtual void ccTouchEnded(CCTouch *pTouch,CCEVENT *pEvent);

    virtual void ccTouchCancelled(CCTouch *pTouch,CCEvent *pEvent);

    获取点击的坐标点

    CCPoint pt = pTouch->getLocation();

    CCLog(“x=%f y=%f”,pt.x,pt.y);

    理解getDelta();

    ccTouchBegan

    start x = 67.309761 y = 180.989975

    ccTouchMoved

    delta x = 1.013191, y = 0.000000

    ccTouchMoved

    delta x = 5.977837, y = 0.000000

    ccTouchMoved

    delta x = 3.985229, y = 0.000000

    ccTouchMoved

    delta x = 1.013184, y = 0.000000

    ccTouchMoved

    delta x = 1.992615, y = 0.000000

    ccTouchMoved

    ...................................

    ...................................

    delta x = 1.013184, y = 0.000000

    ccTouchMoved

    delta x = 0.979431, y = 0.000000

    ccTouchMoved

    delta x = 1.992615, y = 0.000000

    ccTouchMoved

    delta x = 3.005798, y = 0.000000

    ccTouchMoved

    delta x = 3.005798, y = 0.000000

    ccTouchMoved

    delta x = 2.972015, y = 0.000000

    ccTouchMoved

    delta x = 1.013214, y = 0.000000

    ccTouchMoved

    delta x = 3.005798, y = 0.000000

    ccTouchMoved

    delta x = 0.979431, y = 0.000000

    ccTouchEnded

    3 多点触摸

    打开触摸开关,系统默觉得单点触摸。

    setTouchEnabled(true);

    cocos2dx对触摸分三部来处理。各自是点触,移动。离开。或是中间被打断。

    其功能皆有对应的virtual函数进行override的。

    virtual bool ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent);

    virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent);

    virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent);

    virtual void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent);

    获取点击的坐标点。

    4 案例(打飞机)

    Touch.h

    #ifndef __TOUCH_H__

    #define __TOUCH_H__

     

    #include "cocos2d.h"

    USING_NS_CC;

    class Touch :public CCLayerColor

    {

    public:

        static CCScene * scene();

        CREATE_FUNC(Touch);

        bool init();

        enum MyEnum

        {

            BN747

        };

     

        bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);

        void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);

        void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);

        void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);

    };

     

    #endif

    Touch.cpp

    #include "Touch.h"

    #include "AppMacros.h"

     

    CCScene *Touch::scene()

    {

        CCScene * scene = CCScene::create();

        Touch * layer = Touch::create();

        scene->addChild(layer);

        return scene;

    }

     

    bool Touch::init()

    {

        CCLayerColor::initWithColor(ccc4(255, 255, 255, 255));

     

        setTouchEnabled(true);

        setTouchMode(kCCTouchesOneByOne);

     

        CCSprite * plane = CCSprite::create("hero1.png");

        plane->setPosition(ccp(100, 100));

        addChild(plane);

        plane->setTag(BN747);

     

        return true;

    }

     

    bool Touch::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)

    {

        CCSprite * plane = (CCSprite *)getChildByTag(BN747);

        CCPoint pt = pTouch->getLocation();

        if (plane->boundingBox().containsPoint(pt))

        {

            CCLOG("touched");

            return true;

        }

        //决定消息是否往下传递

        return false;

    }

     

    void Touch::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)

    {

        CCSprite * plane = (CCSprite *)getChildByTag(BN747);

        CCPoint pt = plane->getPosition() + pTouch->getDelta();

        if (pt.x > plane->getContentSize().width / 2

            && pt.x < winSize.width - plane->getContentSize().width / 2

            && pt.y > plane->getContentSize().height / 2

            && pt.y < winSize.height - plane->getContentSize().height / 2)

        {

            plane->setPosition(pt);

        }

        CCLog("ccTouchMoved x = %g y = %g", pt.x, pt.y);

    }

     

    void Touch::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)

    {

        CCLog("ccTouchEnded");

    }

     

    void Touch::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent)

    {

        CCLog("ccTouchCancelled");

    }

    执行结果:

     

  • 相关阅读:
    EF6学习
    jQuery中each的用法之退出循环和结束本次循环
    Convert.ToInt16(s);int.Parse(s);和(int)s的区别
    .net网站部署winserver2008R2 iis浏览只显示目录浏览
    二维数组按某列排序
    XML文件的操作
    .net中 IOC DI
    .net在Oracle数据库中为In条件查询防止sql注入参数化处理
    测试代码耗时的时间段(.net)
    沙箱测试
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5328550.html
Copyright © 2020-2023  润新知