locked
Removing from list using Touch. RRS feed

  • Question

  • Every way I've tried so far isn't seeming to work. Can't logically think about it at the moment anymore and decided to break a bit into XNA, so I'm coming to you guys. Blah, seems to have chopped out some of the coding.

    I have a list being populated:

    Random rand = new Random();
    int i = 0;
    while (i
    {
    storedballs.Add(new Vector2(rand.Next(1, 500), rand.Next(1, 500)));
    i++;
    }

    So at this point, 30 "Bubbles" are being displayed on the screen randomly(ignore the coordinates at the moment they're not the screen size, I know :P)

    At this point based on touch if touch.Position is equal to the storedballs I want it to be removed from the list, so it doesn't draw it anymore.
    I've tried doing the update method multiple ways, but it never seems to work. - but this is what I currently have.

    TouchCollection tc = TouchPanel.GetState();
    foreach (TouchLocation touch in tc)
    {
    foreach (Vector2 v2 in storedballs)
    {
    if(touch.Position == v2)
    {
    storedballs.Remove(v2);
    }
    }

    I've never had to unpopulate or populate a list based on touch like this, so I'm really needing some help thinking this out.
    Friday, December 17, 2010 5:17 AM

Answers

  • You can never rely on a touch position being the exact same position as the objects you are testing against.  Test if the touch point is within the area of the ball.  Since you are using balls, you can easily do the test using the squared radius.  If the length of the vector between the touch position and the centre of the ball is less than the radius of the ball, you have a winner.  Use the squared radius because this avoids an expensive square root in calculating the length of the vector.

    float radiusSquared = 10.0f * 10.0f;
    if (touch.State == TouchState.Pressed)
    {
      Vector t = touch.Position - v2;
      if (t.LengthSquared() < radiusSquared)
      {
        storedballs.Remove();
      }
    }

    You may have issues removing an entry in the storedballs list while iterating it, but I'll leave that for you to work out.
    Friday, December 17, 2010 5:29 AM

All replies

  • You can never rely on a touch position being the exact same position as the objects you are testing against.  Test if the touch point is within the area of the ball.  Since you are using balls, you can easily do the test using the squared radius.  If the length of the vector between the touch position and the centre of the ball is less than the radius of the ball, you have a winner.  Use the squared radius because this avoids an expensive square root in calculating the length of the vector.

    float radiusSquared = 10.0f * 10.0f;
    if (touch.State == TouchState.Pressed)
    {
      Vector t = touch.Position - v2;
      if (t.LengthSquared() < radiusSquared)
      {
        storedballs.Remove();
      }
    }

    You may have issues removing an entry in the storedballs list while iterating it, but I'll leave that for you to work out.
    Friday, December 17, 2010 5:29 AM
  • You can never rely on a touch position being the exact same position as the objects you are testing against.  Test if the touch point is within the area of the ball.  Since you are using balls, you can easily do the test using the squared radius.  If the length of the vector between the touch position and the centre of the ball is less than the radius of the ball, you have a winner.  Use the squared radius because this avoids an expensive square root in calculating the length of the vector.

    float radiusSquared = 10.0f * 10.0f;
    if (touch.State == TouchState.Pressed)
    {
      Vector t = touch.Position - v2;
      if (t.LengthSquared() < radiusSquared)
      {
        storedballs.Remove();
      }
    }

    You may have issues removing an entry in the storedballs list while iterating it, but I'll leave that for you to work out.
    Thank you for the quick response.
    Not giving the exact result I expected, so time to throw it into Debug and see whats going on =/
    I appreciate your help - may just have to let it go for tonight and take a look at it tomorrow.


    So did some debugging, and had to do a little adjusting to a few things, but it seems to work good except I'm running into an InvalidOperationException after the process is finished and it attempts to remove an object from the list.
    It's flagging an error at this statement: foreach (Vector2 v2 in storedballs)

    Good lord I'm tired. All I needed was to add break;
    Thank you greatly for your help.
    Friday, December 17, 2010 6:11 AM