cocos2d_x3.0的触屏事件

在cocos2dx 3.0中的触摸代码可以分为两种方式


  可以用一个lambda函数实现具体的lambda 是什么东西可以自己百度

// 使用 lambda 实现 onTouchBegan 事件回调函数
listener1->onTouchBegan = [](Touch* touch, Event* event){
    // 获取事件所绑定的 target 
    auto target = static_cast<Sprite*>(event->getCurrentTarget());
 
    // 获取当前点击点所在相对按钮的位置坐标
    Point locationInNode = target->convertToNodeSpace(touch->getLocation());
    Size s = target->getContentSize();
    Rect rect = Rect(0, 0, s.width, s.height);
 
    // 点击范围判断检测
    if (rect.containsPoint(locationInNode))
    {
        log("sprite began... x = %f, y = %f", locationInNode.x, locationInNode.y);
        target->setOpacity(180);
        return true;
    }
    return false;
    };

其次可以使用如下的函数去做

首先下面 的第一部分中onTouch都是单数形式(这个我之前还真没注意过什么单数形式,而且我就暂且那么叫了),而且第一个onTouchBegan的返回值是bool类型的,其他的都是void的这些都有什么区别呢?

virtual bool onTouchBegan(Touch *touch, Event *unused_event);   
virtual void onTouchMoved(Touch *touch, Event *unused_event);   
virtual void onTouchEnded(Touch *touch, Event *unused_event);   
virtual void onTouchCancelled(Touch *touch, Event *unused_event); 

virtual void onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event);  
virtual void onTouchesMoved(const std::vector<Touch*>& touches, Event *unused_event);  
virtual void onTouchesEnded(const std::vector<Touch*>& touches, Event *unused_event);  
virtual void onTouchesCancelled(const std::vector<Touch*>&touches, Event *unused_event);
 首先是关于这个返回bool的onTouchBegan问题。首先它是属于单数类型,那么什么是单数类型呢?大家都知道现在的手机基本上是点

触控,但是之前的手机一直都是单点触控,那么这个单数类型的就是对应以前的单点触控,也就是说你如果仅仅想做一个只能单点触控

的手机游戏那么你只用这个就行了。

bool onTouchBegan

比较正规的解释是如果返回的是true的话,本层的后续touch事件(指的是onTouchMove等等的)会得到触发,但是会阻后层

传递事件,解释一下什么是向后层传递,因为一般游戏都一层层的贴起来的有用layer贴起来的有用sprite贴起来的,上层会覆盖下,

如果说返回true那么代表着只能是游戏的最上层得到触摸事件,而其他的层得不到的。

那么下来就来点干货吧!

触摸事件首先要在初始化的时候有一个事件分发器

auto dispatcher = Director::getInstance()->getEventDispatcher();  
auto listener = EventListenerTouchOneByOne::create();  
listener->onTouchBegan = CC_CALLBACK_2(GameLayer::onTouchBegan,this);  
listener->onTouchMoved = CC_CALLBACK_2(GameLayer::onTouchMoved,this);  
listener->onTouchEnded = CC_CALLBACK_2(GameLayer::onTouchEnded,this);  
dispatcher->addEventListenerWithSceneGraphPriority(listener,this);

接下来就是关于触摸事件的了。头文件声明就不用说了,

bool Game1_1::onTouchBegan(Touch *touch, Event *event){
auto touchLocation = touch->getLocation();
touchLocation = this->convertToNodeSpace(touchLocation);
if(touchLocation.x>0){
CCLOG("触摸事件响应");
}
return true;
}


其他类型的都大体相同。

接下来就是本文的重点内容就是关于触摸事件的多点响应了

不多说实际点直接贴代码吧!

头文件声明:
void onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event);

cpp中的:

同样的有一个触摸事件的分发器在init初始化中有的,


auto dispatcher = Director::getInstance()->getEventDispatcher();
auto listener1 = EventListenerTouchAllAtOnce::create();
listener1->onTouchesBegan = CC_CALLBACK_2(GameScene1_1::onTouchesBegan, this);
listener1->onTouchesEnded = CC_CALLBACK_2(GameScene1_1::onTouchesEnded, this);
dispatcher->addEventListenerWithSceneGraphPriority(listener1, this);


接下来就是关于



void GameScene1_1::onTouchesBegan( const std::vector<Touch*>&touches, Event *unused_event){
Touch *touch = touches.back();
Touch *touch1 = touches.back();
auto touchLocation = touch->getLocation();
auto touchLocation1 = touch1->getLocation();
touchLocation = this->convertToNodeSpace(touchLocation);
touchLocation1 = this->convertToNodeSpace(touchLocation1);
if (touchLocation.x >0){
CCLOG("touchhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");
}
if (touchLocation1.x >0){
CCLOG("touch11111111111111111111111111111111111");
}

}



这样既可;;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值