触摸事件

来源:互联网 发布:北京软件测试培训班 编辑:程序博客网 时间:2024/05/17 00:52

、单点触摸事件

单点触摸:

首先进入Layer函数中 81行四个方法重写

void onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event);

//触摸开始,当用户的手指接触到手机屏幕时

void onTouchesMoved(const std::vector<Touch*>& touches, Event *unused_event);

//触摸移动,当用户的手指在屏幕上滑动时

void onTouchesEnded(const std::vector<Touch*>& touches, Event *unused_event);

//触摸结束,当用户的手指脱离了手机屏幕

void onTouchesCancelled(const std::vector<Touch*>&touches,Event*unused_event);

//触摸取消,当用户的手指还没有脱离手机屏幕,但是出现了手机来电的情况,使我们的游戏进入了后台

bool HelloWorld::onTouchBegan(Touch *touch,Event *unused_event)//opengl

{

    

    Vec2 v=touch->getLocation();//得到当前的触摸点

    touch->getPreviousLocation();//得到前一个触摸点的坐标

    touch->getStartLocation();//得到第一个点的触摸坐标

    log("%f,%f",v.x,v.y);

    log("0000");

    Rect rect=_sprite->getBoundingBox();

   // rect.intersectsRect(const cocos2d::Rect& rect);两个矩形区域是佛相交

    if(rect.containsPoint(v))//矩形区域是否包含摸个点

    {

        return true;

    }

    return false;//当前对象是否处理

}

 

//1 获取分发中心

    auto d=Director::getInstance()->getEventDispatcher();

//2创建一个单点触摸事件监听器

    auto l=EventListenerTouchOneByOne::create();

    

   //l->setSwallowTouches(true);//可以吞噬

    l->onTouchBegan=CC_CALLBACK_2(HelloWorld::onTouchBegan,this);

    l->onTouchMoved=CC_CALLBACK_2(HelloWorld::onTouchMoved,this);

    l->onTouchEnded=CC_CALLBACK_2(HelloWorld::onTouchEnded,this);

    l->onTouchCancelled=CC_CALLBACK_2(HelloWorld::onTouchCancelled,this);

    //3注册

  //  d->addEventListenerWithSceneGraphPriority(l, this);

    

    d->addEventListenerWithFixedPriority(l, -1);

    _sprite=Sprite::create("CloseNormal.png");

    _sprite->setPosition(Vec2(100,100));

    this->addChild(_sprite);

15、多点触摸事件

 

在iOS aAppController.mm58行[eaglView setMultipleTouchEnabled:true];设置允许多点触摸,否则会把多点当成单点

layer中赋值多点触摸的四个方法

void onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event);

     void onTouchesMoved(const std::vector<Touch*>& touches, Event *unused_event);

     void onTouchesEnded(const std::vector<Touch*>& touches, Event *unused_event);

     void onTouchesCancelled(const std::vector<Touch*>&touches,Event *unused_event);

   //1 获取分发中心

    auto d=Director::getInstance()->getEventDispatcher();

    

    //2创建监听器

    auto l=EventListenerTouchOneByOne::create();

    //l->setSwallowTouches(true);//可以吞噬

    l->onTouchBegan=CC_CALLBACK_2(HelloWorld::onTouchBegan,this);

    l->onTouchMoved=CC_CALLBACK_2(HelloWorld::onTouchMoved,this);

    l->onTouchEnded=CC_CALLBACK_2(HelloWorld::onTouchEnded,this);

    l->onTouchCancelled=CC_CALLBACK_2(HelloWorld::onTouchCancelled,this);

    

    //3注册

    d->addEventListenerWithSceneGraphPriority(l,this);

 

    Sprite* _sprite=Sprite::create("HelloWorld.pgn");

    _sprite->setPosition(Vec2(240,160));

    this->addChild(_sprite);

    return true;

}

 

 

void HelloWorld::onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event)

{

    if(touches.size()==2)

    {

        Touch* touch=touches.at(0);

        Vec2 v=touch->getLocation();

        Touch* touch2=touches.at(0);

        Vec2 v2=touch2->getLocation();

        _distance=v.getDistance(v2);//获得两个点之间的距离

    }

    

    

    log("HelloWorld::onTouchesBegan");

}

void HelloWorld::onTouchesMoved(const std::vector<Touch*>& touches, Event *unused_event)

{

    if (touches.size()==2) {

        Touch* touch=touches.at(0);

        Vec2 v=touch->getLocation();

        Touch* touch2=touches.at(0);

        Vec2 v2=touch2->getLocation();

        float distance=v.getDistance(v2);

        float r=distance/_distance;

       

        _sprite->setScale(_sprite->getScale()*r);

    }

}

void HelloWorld::onTouchesEnded(const std::vector<Touch*>& touches, Event *unused_event)

{

}

void HelloWorld::onTouchesCancelled(const std::vector<Touch*>&touches,Event *unused_event)

{

}

 

多点触摸和单点触摸的区别: 

(1)多点默认是不开启的,所以需要先开启一下 (2)多点触摸一定会接受这次的触摸事件,但是单点触摸可以选择是否接受 (3)单点触摸可以有吞噬,多点触摸不能吞噬 

(4)单点触摸的优先级永远比多点触摸的高 

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 新插座插不进去怎么办 本溪人社app打不开怎么办 南宁电车超过上牌时间怎么办 苹果6s降频怎么办 0首付手机还不起怎么办 乐才app登录不上怎么办 买了笔记本网要怎么办? 电脑连接不上网络怎么办 电脑上没网络了怎么办 手机返回键不好使怎么办 笔记本无线网连接受限怎么办 魅族打电话图标没有了怎么办 京东价格保护后发票怎么办 淘宝未满十八岁怎么办 SVN提交时代码冲突怎么办 京东自营没货了怎么办 京东下了单 没货怎么办 万达广场购物卡怎么办 京东白条没额度怎么办 京东退款还收到货怎么办 退货不想要货了怎么办 安装微擎创建数据库失败怎么办 tp文件损坏或丢失怎么办 手机QQ启动有问题应该怎么办 微信网页版页面巨大怎么办 魔兽军团烹饪食谱扔了怎么办 赴港旅游网上签证怎么办了? 美团商家入住没有营业执照怎么办 团购房子不想要怎么办 乐视液晶屏坏了怎么办 wifi盒子插上网线然后怎么办 在泰国手机坏了怎么办 朋友在泰国手机关机怎么办 qq上转账转错了怎么办 扣扣转账转错了怎么办 到付快递签收了怎么办 京东白条消费了怎么办 确认收货点错了怎么办 小米6后盖缝隙大怎么办 苹果6splus电池不耐用怎么办 苹果一朵云加箭头下载不了怎么办