我是否需要为每个要检测到的每个精灵设置AddEventListenerWithSceneGrapriority
Do I need to set addEventListenerWithSceneGraphPriority for each sprite I want to detect?
我想检测到触摸了哪个精灵。如果我这样做:
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), mySprite);
然后在我的触摸方法中我做:
bool HelloWorld::onTouchBegan(Touch* touch, Event* event)
auto spriteBlock = static_cast<Block*>(event->getCurrentTarget());
发现精灵的良好。
问题是我在层上有20个精灵,我需要能够检测到它们我需要设置
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), mySprite);
每个精灵?
不,您不需要将EventListener添加到所有精灵中。
您需要一个sprites的父节点的事件侦听器。
尝试以下操作:
bool HelloWorld::onTouchBegan(Touch *touch, Event *event) {
Node *parentNode = event->getCurrentTarget();
Vector<Node *> children = parentNode->getChildren();
Point touchPosition = parentNode->convertTouchToNodeSpace(touch);
for (auto iter = children.rbegin(); iter != children.rend(); ++iter) {
Node *childNode = *iter;
if (childNode->getBoundingBox().containsPoint(touchPosition)) {
//childNode is the touched Node
//do the stuff here
return true;
}
}
return false;
}
它是相反的顺序迭代,因为您将触摸到适当的最高z索引的精灵(如果它们重叠)。
希望,这个帮助。
是的,您必须向每个精灵添加一个eventlistener并检测每个触摸您必须在触摸中添加此功能
bool OwlStoryScene0::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event) {
cocos2d::Touch *touched = (Touch *) touch;
Point location = touched->getLocationInView();
location = Director::getInstance()->convertToGL(location);
auto target = static_cast<Sprite*>(event->getCurrentTarget());
Point locationInNode = target->convertToNodeSpace(touch->getLocation());
Size s = target->getContentSize();
Rect rect = Rect(0, 0, s.width, s.height);
if (rect.containsPoint(locationInNode) && target == SPRITE) {
//DoSomething
}
else if (rect.containsPoint(locationInNode) && target == SPRITE) {
//DoSomething
}
return false;
}
希望这对您有用
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 在C++/Linux中设置单调时钟的一些技巧
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 嵌套在类中时无法设置成员数据
- 需要帮助设置在C++中使用的Potrace
- 如何在自删除后将对象设置为nullptr
- 将指针设置为"nullptr"并不能防止双重删除?
- 如何在Ubuntu中使用cmake设置qt4
- ld:bind_at_load和-bitcode_bundle(Xcode设置ENABLE_bitcode=YES)不能
- 如何在boost beast http请求中设置http头
- 有没有办法在链接时设置精灵需要字段?
- 在 Cocos2dx 3.0 中的精灵上设置 TexParams
- 我是否需要为每个要检测到的每个精灵设置AddEventListenerWithSceneGrapriority
- [Cocos2dx]如何为自定义精灵设置图像
- cocos2dx精灵设置位置错误
- Cocos2d-x:当设置PhysicsWorld时,一些精灵会变得不可见
- SDL如何在按键时设置精灵的动画
- 将轻扫事件设置为精灵
- DarkGDK,在精灵刷出时间之间设置一个间隔
- 我如何设置一个if语句来动画一个精灵