获取CCScrollview中触摸的位置
Getting position of a touch in a CCScrollview
我在屏幕一侧有一个充满CCSprites的滚动视图,我希望能够将其中一个拖动到屏幕的主区域。我只需要找到用户开始拖拽的精灵。
我试着将触摸位置移动到每个精灵的坐标空间,但是数字到处都是。
位置为Touch->getStartLocationInView()
ScrollViewItems是Vector<Sprite*>
string HelloWorld::SpriteNameForPosition(cocos2d::Vec2* position)
{
for(Vector<Sprite*>::iterator iter = scrollViewItems.begin() ;iter != scrollViewItems.end();iter++)
{
Sprite* sprite = *iter;
Vec2 spriteTouchPos = sprite->convertToNodeSpace(*position);
Rect bounds = Rect(0, 0, sprite->getBoundingBox().size.width, sprite->getBoundingBox().size.height);
if(bounds.containsPoint(spriteTouchPos))
{
return names[sprite->getTag()];
}
}
return "";
}
第一个问题是为什么要改变boundingBox的位置?而不是将你的位置设置为(0,0),只需获取boundingBox并检查该点是否包含在其中。
滚动视图将更新到内容/boundingBox的实际位置时,偏移量应用于所有子框,所以边界框将始终反映正确的位置,因为你在屏幕上看到。你可以通过使用drawNode在精灵周围绘制矩形来检查这一点。我通常将此作为调试的一种方式,以确保我正在使用正确的尺寸和位置。
你可以通过使用boundingBox来修复你的问题,因为它是从每个scrollViewItem返回的。
auto bounds = sprite->getBoundingBox();
if(bounds.containsPoint(position))
{
return sprite->getName();
}
相关文章:
- 将值指定给向量(2D)的向量中的某个位置
- 使用Unreal C++获取VR耳机的世界位置/方向
- 写入位置0x0000000C时发生访问冲突
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- GL_SHADERSTORAGE_BUFFER位置是否与其他着色器位置冲突
- 如何使用cudaMallocManaged在指针位置初始化对象?(C++)
- 无法获取webot::PositionSensor对象中位置传感器的值
- 非常量变量只读位置的赋值
- 如何定义更改car类中car位置的方法
- 使用迭代器时如何访问对象在向量中的位置?
- 更改.cpp程序的输入文件中数据的位置会意外更改输出
- 求最大元素位置的分治算法
- 你好。。。id_public变量不应该给出结果为 81 和 86 吗?为什么它为两个派生类占用不同的内存位置?
- 为什么我的点没有在 OpenGL 中绘制鼠标所在的位置?
- 是否可以从触摸板获得绝对触摸位置
- Direct3D 中的 Windows Phone 8 触摸位置
- 根据触摸位置确定移动方向
- 如何在Cocos2dx V3.8中获得触摸位置
- 在触摸板上获得手指位置
- 获取CCScrollview中触摸的位置