I have 2 dimensional vector of sprites. Each sprite have EventListenerTouchOneByOne
auto _touchListener = EventListenerTouchOneByOne::create();
_touchListener->setSwallowTouches(true);
_touchListener->onTouchBegan = [&](Touch* touch, Event* event)
{
Vec2 p = touch->getLocation();
Rect rect = this->getBoundingBox();
if (rect.containsPoint(p))
{
return true;
}
return false;
};
_touchListener->onTouchEnded = [=](cocos2d::Touch* touch, cocos2d::Event* event)
{
Card::touchEvent(touch);
};
I add sprites on scene but EventListener not working. Why? Sorry. My English bad.
are you sure that rect.containsPoint(p) is returning true?
Oh. I’ts return false. But now EventListener works only on last added on scene sprite. If i touch him or other sprite. Work only one
Did you add this event to each Sprite you want it to respond for?
I think you need one listener per object, otherwise the lambda that captures the this pointer and where you get the rect from just overwrites the previous one.
I have class Card which is inherited from Node. Card contain 2 objects. Sprite and Label. In Card class i add EventListenerTouchOneByOne. I post it before.
In Board class i add cards in loop.
Card* card = Card::create(((i * ROW + j) + 2) / 2);
card->setPosition(_cardsPosition[i * ROW + j].x * DISTANCE_BETWEEN_CARD, _cardsPosition[i * ROW + j].y * DISTANCE_BETWEEN_CARD);
card->setPositionOnTable(_cardsPosition[i * ROW + j].x, _cardsPosition[i * ROW + j].y);
card->hide();
addChild(card, 1);
I don’t see you adding an event to card
Events add in init funcrion in card class.
bool Card::init(int value)
{
if (!Node::init())
{
return false;
}
auto _touchListener = EventListenerTouchOneByOne::create();
_touchListener->setSwallowTouches(true);
_touchListener->onTouchBegan = [&](Touch* touch, Event* event)
{
auto target = static_cast<Card*>(event->getCurrentTarget());
Point locationInNode = target->convertToNodeSpace(touch->getLocation());
Size s = target->getContentSize();
Vec2 p = touch->getLocation();
//Rect rect = this->getBoundingBox();
Rect rect = Rect(0, 0, s.width, s.height);
if (rect.containsPoint(p))
{
return true;
}
return false;
};
_touchListener->onTouchEnded = [=](cocos2d::Touch* touch, cocos2d::Event* event)
{
Card::touchEvent(touch);
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(_touchListener, this);
This doesn’t look like something I am used to seeing.
This is something I do in one of my projects
auto listener = cocos2d::EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = [&](cocos2d::Touch* touch, cocos2d::Event* event)
{
cocos2d::Vec2 p = touch->getLocation();
cocos2d::Rect rect = this->getBoundingBox();
if(rect.containsPoint(p))
{
CornSprite::touchEvent(touch);
return true;
}
return false;
};
listener->onTouchEnded = [=](cocos2d::Touch* touch, cocos2d::Event* event)
{
};
cocos2d::Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
Isn’t the touch in screen space, so it needs to be converted to node space?
I’am trying do like you say. It’s not working