Reputation: 2231
I have three sprites on my scene. I have to make a touch event wherein the player can drag one of the sprites. My problem is these sprites are very thin and whenever I try to drap one of them, the bounding box appear to be very big so even if I touch the empty space and drag, the sprite would move. Here's how I was trying to do it.
NSSet *allTouch = [event allTouches];
UITouch *touch = [[allTouch allObjects]objectAtIndex:0];
CGPoint loc = [touch locationInView:[touch view]];
loc = [[CCDirector sharedDirector]convertToGL:location];
//Swipe Detection - Beginning point
beginTouch = location;
for(int i = 0; i < [objArray count]; i++)
{
CCSprite *sprite = (CCSprite *)[objArray objectAtIndex:i];
CGRect spriteRect = CGRectMake(sprite.position.x - (sprite.contentSize.width/2), sprite.position.y - (sprite.contentSize.height/2), sprite.contentSize.width, sprite.contentSize.height);
if(CGRectContainsPoint(spriteRect, location))
{
//actions here
}
}
How do I set the bounding box to be exactly the size of the sprite?
Upvotes: 0
Views: 2508
Reputation: 479
You can do it like this:
CCSprite * sprite = [CCSprite spriteWithFile:@"sprite.png"];
CGRect boundingBox = sprite.boundingBox;
However if your image file contains transparent space around your sprite, that will also be part of the bounding box.
Upvotes: 1