1

I have a scrollview at one side of the screen filled with CCSprites and I want to be able to drag one of them into the main area of the screen. I just need to find which Sprite the user started dragging on.

I have tried to move the Touch location into each Sprites coordinate space but the numbers are all over the place.

position is the Touch->getStartLocationInView()

ScrollViewItems is a 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 "";
}
Conor
  • 1,007
  • 1
  • 10
  • 16

1 Answers1

0

The first question is why are you changing the position of your boundingBox? instead of setting you position to (0, 0), just get boundingBox and check if the point is contained within that.

The scroll view will update to actual position of the content/boundingBox when the offset is applied to all children, so the bounding box will always reflect the correct position on as you're seeing on screen. You can check this by using a drawNode to draw your rectangles around your sprites. I usually use this as a way of debugging to make sure I'm working with the correct dimensions, and positions.

You can fix your problem by just using the boundingBox as it's returned from each scrollViewItem.

auto bounds = sprite->getBoundingBox();

if(bounds.containsPoint(position))
{
   return sprite->getName();
}
Filled Stacks
  • 4,116
  • 1
  • 23
  • 36