首页 > 代码库 > cocos2dx 3.2 事件机制

cocos2dx 3.2 事件机制

一个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<Sprite*>(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<Sprite*>(event->getCurrentTarget());        target->setPosition(target->getPosition() + touch->getDelta());    };    listener1->onTouchEnded = [=](Touch* touch, Event* event){        auto target = static_cast<Sprite*>(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 

二:两个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<Sprite*>(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<Sprite*>(event->getCurrentTarget());		target->setPosition(target->getPosition() + touch->getDelta());	};	listener1->onTouchEnded = [=](Touch* touch, Event* event){		auto target = static_cast<Sprite*>(event->getCurrentTarget());		log("sprite onTouchesEnded.. ");		target->setOpacity(255);		};	_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite2);}

结论:

1,a.addChild(b); a的宽高没变,还是自己的宽高。

//sprite包含情况void HelloWorld::testSpriteAdd(){    auto sprite1 = Sprite::create("Images/CyanSquare.png");    sprite1->setPosition(160, 240);        log("sprite1 w,h %f %f",sprite1->getContentSize().width, sprite1->getContentSize().height);    auto node = Node::create();    node->addChild(sprite1);    addChild(node, 0, 100);    log("node w,h %f %f", node->getContentSize().width, node->getContentSize().height);//0,0}

2.node.addChild(sprite);node的宽和高也没变 感觉2dx的显示不是树形结构。

 层级添加  不会改变原层大小。