Help with Touch Event firing only on the first object of the List.

Topics: Developer Forum, Project Management Forum, User Forum
Nov 5, 2012 at 3:19 PM
Edited Nov 5, 2012 at 3:20 PM

 

// I have this list
List<Crate> _crates = new List<Crate>();

// and I created two Crate Object and push them in the list
Crate crate1 = new Crate(this, World, Statics.UNBREAKABLE);
crate1._body.Position = Vector2.Zero;
_crates.Add(crate1);

Crate crate2 = new Crate(this, World, Statics.BREAKABLE);
crate2._body.Position = new Vector2(0f, -7f);
crate2._body.BodyType = BodyType.Dynamic;
_crates.Add(crate2);

// this is inside my update function
// only the first object can recieve the event
// if I reverse the adding of Items it will still fire on the first item

foreach (Crate crate in _crates)
{
    if (IsTouched(crate._body, crate._sprite, Camera))
    {
        Debug.WriteLine("touched");
    }
}

// the IsTouched function

public static bool IsTouched(Body body, Sprite sprite, Camera2D camera)
{
    bool touched = false;

    TouchCollection touchLocations = TouchPanel.GetState();

    foreach (TouchLocation touchLocation in touchLocations)
    {
        if (touchLocation.State == TouchLocationState.Pressed)
        {
            Vector2 objectPositionSim = camera.ConvertWorldToScreen(body.Position);

            if (touchLocation.Position.X >= (objectPositionSim.X - (sprite.Texture.Width / 2)) &&
                touchLocation.Position.X <= (objectPositionSim.X + (sprite.Texture.Width / 2)) &&
                touchLocation.Position.Y >= (objectPositionSim.Y - (sprite.Texture.Height / 2)) &&
                touchLocation.Position.Y <= (objectPositionSim.Y + (sprite.Texture.Height / 2)))
            {
                touched = true;
            }
        }
    }

    return touched;
}
can anybody see any problem with my code? Thanks guys.
Coordinator
Nov 9, 2012 at 1:43 AM

Instead of checking against the rectangle texture, you can use World.TestPoint() or Fixture.TestPoint() to check for collisions.

What you are basically doing inside IsTouched, is testing if any of the touch locations on the screen, are inside the boundaries of the texture. However, for this to work with TestPoint, you need to convert the touch location to world coordinates first. If what you need, is a list of bodies that are touched, you could do something like this:

List<Crate> _crates = new List<Crate>();

Crate crate1 = new Crate(this, World, Statics.UNBREAKABLE);
_crates.Add(crate1);

Crate crate2 = new Crate(this, World, Statics.BREAKABLE);
crate2._body.Position = new Vector2(0f, -7f);
crate2._body.BodyType = BodyType.Dynamic;
_crates.Add(crate2);

TouchCollection touchLocations = TouchPanel.GetState();

List<Body> touchedBodies = new List<Body>();

foreach (TouchLocation touchLocation in touchLocations)
{
    if (touchLocation.State == TouchLocationState.Pressed)
    {
        Vector2 position = camera.ConvertWorldToScreen(touchLocation.Position);

         touchedBodies.Add(World.TestPoint(position).Body);
    }
}

 

And now touchedBodies will contain all the bodies that have been touched. Note: The code snippet editor does not work for me, and I wrote the code by hand, so you might need to do some adjustments.

Nov 9, 2012 at 4:43 AM
Edited Nov 9, 2012 at 5:03 AM

[SOLVED]

It works great and Thank you GenBox! but my other problem right now is how can I reference my actual Class Object.

 

example: I have the class Crate and the instance object of it is _crate

and the Crate class has a public function of Jump()

Now if I use the above code, I can iterate through all the bodies.

So now I don't know to determine if the current body that I have iterated is the body of my _crate object. and I can't call the Jump() method

Nov 9, 2012 at 5:00 AM

I'm so dumb. Sorry! I got the solution. I never thought I could do this and will work

if (touchedFixture.Body == _ball._body)
{
      Debug.WriteLine("touched the ball");
}
else if (touchedFixture.Body == _wood._body)
{
      Debug.WriteLine("touched the wood");
}

I thought I need something like instanceof function of Java.

Sorry I'm just beginning Game Development. I was a Systems Developer.