如何让精灵吞下所有触碰
cocos2d-x 3 How to make sprite swallow all touches only on it
本文关键字:精灵 更新时间:2023-10-16
我有一个main layer
应该得到触摸事件。但在那一层有一个navigation bar
,其中有按钮和其他精灵,是cocos2d::Sprite
的子类。现在我需要所有对navigation bar
的触摸都不能被解释为对main layer
的触摸。按钮正常工作,但navigation bar
精灵将触摸传递给main layer
。我这样做是为了防止传递事件:
auto touchListenerOneByOne = EventListenerTouchOneByOne::create();
touchListenerOneByOne->setSwallowTouches(true);
touchListenerOneByOne->onTouchBegan = CC_CALLBACK_2(NavigationBar::onBoardTouchBegan, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListenerOneByOne, this);
bool NavigationBar::onBoardTouchBegan(Touch* touch, Event* event)
{
CCLOG("Navigation sprite is touched......!");
return true;
}
这可以阻止传递,但它会阻塞-吞噬所有的触摸。现在我不能传递任何触摸到main layer
,即使我不触摸navigation bar
。我试图使用setContentSize
,但它没有帮助。解决方案在哪里?
答案在这里:http://www.cocos2d-x.org/wiki/How_To_Subclass_Sprite_And_Add_Event_Listeners
我在这里修改了以下内容:
-
Vector2
toVec2
, - 删除
void touchEvent(cocos2d::Touch* touch, cocos2d::Vector2 _p);
中的第二个参数 -
MySprite::touchEvent(touch);
到touchEvent(touch);
-
cocos2d::Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(listener, 30);
到_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
这使得精灵应该检查触摸是否在它上面,然后在onTouchBegan
监听器中返回true
,否则返回false
。
相关文章:
- OpenGL 着色器不绘制任何东西 - 糟糕的精灵类
- 从精灵表在 sfml 中为精灵制作动画
- AABB 碰撞使实际精灵的大小翻倍
- 如何在精灵表上的两个不同部分之间来回切换,同时用户仍使用 SFML 持有密钥
- 我的精灵使用名为 ASGE 的C++框架卡在屏幕边缘
- 如何为精灵编写复制构造函数
- 绘制精灵会导致分割错误
- 精灵动画查看器应该使用哪些Qt小部件
- Eclipse构建但不会启动精灵
- OpenGL 2D游戏只绘制第二个精灵纹理而不是第一个
- 打开 gl 精灵类不起作用
- 有没有办法在链接时设置精灵需要字段?
- 精灵没有动画
- 在不同位置渲染相同精灵的数组
- 如何在 SFML 上添加更多精灵?
- 精灵不显示我希望它们显示的方式
- 为什么精灵在 cocos2d-x 中颤抖
- 我没有看到精灵的动画
- 为什么该代码不绘制精灵C++ SFML
- 添加精灵时窗口关闭