一个sprite的情况
// oneSpritevoid HelloWorld::touchableSpriteTestOne(){ Vec2 origin = Director::getInstance()->getVisibleOrigin(); Size size = Director::getInstance()->getVisibleSize(); auto sprite1 = Sprite::create("Images/CyanSquare.png"); sprite1->setPosition(160, 240); addChild(sprite1,0,100); // Make sprite1 touchable auto listener1 = EventListenerTouchOneByOne::create(); listener1->setSwallowTouches(true);//设置是否想下传递触摸 listener1->onTouchBegan = [=](Touch* touch, Event* event){ auto target = static_cast(event->getCurrentTarget()); log("target.x.y: %f %f tag:%d isSprite1:%d width:%f,height:%f", target->getPosition().x, target->getPosition().y, target->getTag(), target == sprite1, target->getContentSize().width, target->getContentSize().height); Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation()); log("locationInNode %f %f", locationInNode.x, locationInNode.y); Size s = target->getContentSize(); Rect rect = Rect(0, 0, s.width, s.height); if (rect.containsPoint(locationInNode)) { log("sprite began... x = %f, y = %f tag:%d", locationInNode.x, locationInNode.y, target->getTag()); target->setOpacity(180); return true; } return false; }; listener1->onTouchMoved = [](Touch* touch, Event* event){ log("%s", "onTouchMoved"); auto target = static_cast (event->getCurrentTarget()); target->setPosition(target->getPosition() + touch->getDelta()); }; listener1->onTouchEnded = [=](Touch* touch, Event* event){ auto target = static_cast (event->getCurrentTarget()); log("sprite onTouchesEnded.. "); target->setOpacity(255); }; //onTouchBegan 随便点个地方都触发,onTouchMoved onTouchEnded 是点到sprite1才触发, _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1);}
1、onTouchBegan无论点哪里都触发。只有在点中了sprite1,onTouchBegan return true的时候onTouchMoved ,onTouchEnded才会触发
2、Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation()); 这个touch->getLocation是整个GL的坐标,整个屏幕相对于左下角的坐标
转换为target的本地坐标 矩形是原点为(0,0),长宽,原点是左下角,锚点是中心,addchild的时候是孩子的锚点对准自己的原点,自己的原点是左下角。3、点击屏幕的每个地方 target都是最上层的那个target,通过转换为target的矩形范围,来判断是点击了哪个sprite.
4、
设备是把屏幕上的点击通过坐标告诉你,所以begin必定触发
begin返回true,表示你要处理这个点击,才会触发后面的move和end
5、摸监听listener的创建方式有两种,一种是:EventListenerTouchOneByOne,另一种是:EventListenerTouchAllAtOnce,顾名思义,EventListenerTouchOneByOne的意思单点触摸,EventListenerTouchAllAtOnce,是多点触摸,而不需要再用设置Delegate的方式来做了。3.0触摸机制还有个不同的地方,只要是放在最上面的那个精灵,那它的触摸优先级就最高。我们用的按钮Menu 就是用这种方式设置触摸优先级的。
二:两个sprite 互相包含的情况:
// TouchableSpriteTestvoid HelloWorld::touchableSpriteCont(){ Vec2 origin = Director::getInstance()->getVisibleOrigin(); Size size = Director::getInstance()->getVisibleSize(); auto sprite2 = Sprite::create("Images/MagentaSquare.png"); sprite2->setPosition(origin + Vec2(size.width / 2, size.height / 2)); addChild(sprite2, 20, 200); log("1sprite2.width,sprite2.height: %f %f", sprite2->getContentSize().width, sprite2->getContentSize().height); //锚点确定精灵自己在父节点的加载位置,原点始终在左下角为0,0 addchild的时候是孩子的锚点对准 自己的原点。 //并不是你点精灵的区域才响应 是精灵所在的那一层范围都能响应 auto sprite3 = Sprite::create("Images/YellowSquare.png"); sprite3->setPosition(0, 0); sprite2->addChild(sprite3, 1, 300); log("layer.width,layer.height: %f %f",this->getContentSize().width,this->getContentSize().height); log("2sprite2.width,height:%f %f", sprite2->getContentSize().width, sprite2->getContentSize().height); // Make sprite1 touchable auto listener1 = EventListenerTouchOneByOne::create(); listener1->setSwallowTouches(true); listener1->onTouchBegan = [=](Touch* touch, Event* event){ auto target = static_cast(event->getCurrentTarget()); log("target.x.y: %f %f tag:%d width:%f,height:%f", target->getPosition().x, target->getPosition().y, target->getTag(), target->getContentSize().width, target->getContentSize().height); Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation()); Size s = target->getContentSize(); Rect rect = Rect(0, 0, s.width, s.height); log("locationInNode.x= %f ,y= %f,contentW= %f,contentH= %f", locationInNode.x, locationInNode.y, s.width, s.height); if (rect.containsPoint(locationInNode)) { log("dist began... x = %f, y = %f tag:%d", locationInNode.x, locationInNode.y, target->getTag()); target->setOpacity(180); return true; } return false; }; listener1->onTouchMoved = [](Touch* touch, Event* event){ log("%s", "onTouchMoved"); auto target = static_cast (event->getCurrentTarget()); target->setPosition(target->getPosition() + touch->getDelta()); }; listener1->onTouchEnded = [=](Touch* touch, Event* event){ auto target = static_cast (event->getCurrentTarget()); log("sprite onTouchesEnded.. "); target->setOpacity(255); }; _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite2);}