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?
views:
59answers:
2I 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.
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.