Cocos2dx单击事件优先级实例

前端之家收集整理的这篇文章主要介绍了Cocos2dx单击事件优先级实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
bool MyTouchEvent1::init() { if (!Layer::init()) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); Sprite * sp1 = Sprite::create("HelloWorld.png"); sp1->setPosition(Point(visibleSize.width/2,visibleSize.height/2)); this->addChild(sp1); Sprite * sp2 = Sprite::create("node.png"); sp2->setPosition(Point(visibleSize.width / 2,visibleSize.height / 2)); this->addChild(sp2); //创建单机屏幕的监听器 auto listener = EventListenerTouchOneByOne::create(); //按下事件 listener->onTouchBegan = [](Touch *touch,Event * event) { //注册监听事件时绑定一个node对象,取出这个node对象 auto target = static_cast<Sprite*>(event->getCurrentTarget()); //坐标系转换 Point pos = Director::getInstance()->convertToGL(touch->getLocationInView()); //判断是不是点击到了精灵 if (target->getBoundingBox().containsPoint(pos)) { target->setOpacity(100); return true; } return false; }; //抬起事件 listener->onTouchEnded = [](Touch *touch,Event * event) { //恢复精灵的透明度 auto target = static_cast<Sprite*>(event->getCurrentTarget()); target->setOpacity(255); }; //设施点击的吞没事件(设置了以后,点击重叠部分 不会影响到下一层) listener->setSwallowTouches(true); //注册监听事件 _eventDispatcher->addEventListenerWithSceneGraPHPriority(listener,sp1); _eventDispatcher->addEventListenerWithSceneGraPHPriority(listener->clone(),sp2); return true; }

猜你在找的Cocos2d-x相关文章