五、触摸事件的吞噬和传递

五、触摸事件的吞噬和传递,第1张

概述触摸事件是做cocos2d-x的基础。比如做新手引导。物品的拖动。d窗的屏蔽层。诸如此类.做触摸事件,不要抱着试一试的心态下手,一定要弄透彻,不然出现问题了,你再试一试其实时间花的更多 触摸事件主要需要弄清的事情 是否吞噬setSwallowTouches onTouchBegin返回的意义 代码如下: #include "HelloWorldScene.h"USING_NS_CC;Sce

触摸事件是做cocos2d-x的基础。比如做新手引导。物品的拖动。d窗的屏蔽层。诸如此类.做触摸事件,不要抱着试一试的心态下手,一定要弄透彻,不然出现问题了,你再试一试其实时间花的更多

触摸事件主要需要弄清的事情 是否吞噬setSwallowtouches ontouchBegin返回的意义 代码如下:
#include "HelloWorldScene.h"USING_NS_CC;Scene* HelloWorld::createScene(){    auto scene = Scene::create();    auto layer = HelloWorld::create();    scene->addChild(layer);    return scene;}bool HelloWorld::init(){    if ( !Layer::init() )    {        return false;    }    Size winSize = Director::getInstance()->getWinSize();    auto sprite = Sprite::create("HelloWorld.png");    sprite->setposition(Vec2(winSize.wIDth / 2,winSize.height / 2 ));    this->addChild(sprite,0);    m_nvShenSp = Sprite::create("nvshen.jpg");    m_nvShenSp->setposition(Vec2(winSize.wIDth / 2,winSize.height / 2));    this->addChild(m_nvShenSp);    addtouchEvent();    return true;}//添加触摸事件voID HelloWorld::addtouchEvent(){    auto dispatcher = Director::getInstance()->getEventdispatcher();    auto Listener1 = EventListenertouchOneByOne::create();    Listener1->ontouchBegan = CC_CALLBACK_2(HelloWorld::ontouchBegan,this);    Listener1->ontouchmoved = CC_CALLBACK_2(HelloWorld::ontouchmoved,this);    Listener1->ontouchended = CC_CALLBACK_2(HelloWorld::ontouchended,this);    Listener1->setSwallowtouches(true);//设置吞噬按钮    //给Layer添加触摸事件    dispatcher->addEventListenerWithSceneGraPHPriority(Listener1,this);};bool HelloWorld::ontouchBegan(touch *touch,Event *unused_event){    log("ontouchBegan");    Point touchPoint = touch->getLocation();    Rect nvShenRect = m_nvShenSp->getBoundingBox();//这个函数的内部,其实是给你一个Rect大小,一个在父类的Point    if (nvShenRect.containsPoint(touchPoint)) //如果点击的点落在女神图片上(因为他们同属于一个父控件layer所以可以这么转换,否则请查看坐标转换)    {        return false;//false就往下传递,被按钮吃掉,ontouchmoved,ontouchended接收不到    }    return true; //如果没有点击到女神的图片上,就不往下传递,不往下传递,被ontouchmoved,ontouchended接收};voID HelloWorld::ontouchmoved(touch *touch,Event *unused_event){    log("ontouchmoved");};voID HelloWorld::ontouchended(touch *touch,Event *unused_event){    log("ontouchended");};voID HelloWorld::ontouchCancelled(touch *touch,Event *unused_event){};
总结

以上是内存溢出为你收集整理的五、触摸事件的吞噬和传递全部内容,希望文章能够帮你解决五、触摸事件的吞噬和传递所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1069090.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存