我在屏幕的一侧有一个滚动视图,里面装有CCSprites,我希望能够将其中之一拖到屏幕的主要区域。我只需要找到用户开始拖动哪个Sprite。
我尝试将Touch位置移动到每个Sprites坐标空间中,但是数字到处都是。
位置是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并检查其中是否包含该点即可。
当将偏移量应用于所有子级时,滚动视图将更新为content / boundingBox的实际位置,因此,边框将始终在屏幕上显示正确的位置。您可以通过使用drawNode在精灵周围绘制矩形来进行检查。我通常将其用作调试的方式,以确保使用正确的尺寸和位置。
您可以通过使用从每个scrollViewItem返回的boundingBox来解决问题。
auto bounds = sprite->getBoundingBox();
if(bounds.containsPoint(position))
{
return sprite->getName();
}
关于c++ - 在CCScrollview中获取触摸的位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31609358/