views:

59

answers:

2
+2  Q: 

small sprite touch

There are some games which have some small pictures as sprites and can move by touch.If the sprite is a larger picture,the touch is quite normal.And we can use the function CGRectContainsPoint check for the sprite.But when the sprite is quite small,this function looks not good.Is there other method can sovle the problem?

+1  A: 

I wrote about an approach that you may be able to use to mitigate the "fat finger" issue which @FrustratedWithFormsDesigner described:
http://codecube.net/2010/03/approximating-touch-points/

... what if you compare the user’s touch point against the entity positions and simply select the closest one (regardless of whether it’s 35 or 36 pixels away)?

The post is written in C# for the windows phone, but the concept should apply. Basically, do a distance comparison between the user's touch and the entities on screen. Make the touch manipulate the closest one by distance and you get away from issues of having rigidly defined squares with which to detect touches.

Joel Martinez
This is a good approach, not only because it allows you to touch small sprites, but it's a manner for disambiguating several small sprites in a small area (i.e., choose the closest one). You will still probably want to cap the distance you look; selecting a sprite 250 pixels away will feel strange in your app.
cc
A: 

You can allow touches in a margin around the sprite. e.g. If the touch is within 10 pixels of the sprite, treat it as if it is touching the sprite.

One way of doing this is to expand the sprite's rect before calling CGRectContainsPoint. The following code will expand the rect by 10 for both x and y:

CGRect expanded = CGRectInset(spriteRect, -10.0, -10.0);

Another way of doing it is to create a rect around the touch and use CGRectIntersectsRect to check if the touch rect overlaps with the sprite.

Colin Gislason