Cocos2d iphone 如何检测圆内的触摸

Cocos2d iphone 如何检测圆内的触摸,cocos2d-iphone,Cocos2d Iphone,我真的需要你的帮助。我有点困惑。 我有一个圆形精灵,这个代码 -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { CGSize winSize =[[CCDirector sharedDirector] winSize]; UITouch* myTouch = [touches anyObject]; CGPoint location = [myTouch locationInView: [myTouch v

我真的需要你的帮助。我有点困惑。 我有一个圆形精灵,这个代码

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{

CGSize winSize =[[CCDirector sharedDirector] winSize];
UITouch* myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView: [myTouch view]];
location = [[CCDirector sharedDirector]convertToGL:location];

CCSprite *circleSprite = (CCSprite*)[self getChildByTag:30];
CGRect correctColorSprite1 = [circleSprite boundingBox];

   if (CGRectContainsPoint(correctColorSprite1, location)) {
   NSLog(@"inside");

}
正如我所知,有一个边界框,当我稍微触摸顶部圆圈的外侧时,它仍然会检测到触摸

我在一些论坛上读到,我需要检测精灵中心和接触点的距离。但我真的不知道如何编写代码。我的圆圈大小约为50点

我希望有人能帮助我,给我一些改进的代码片段,只在圆圈中检测触摸。不使用边界框。您的帮助非常充分。

试试这个:

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{

CGSize winSize =[[CCDirector sharedDirector] winSize];
UITouch* myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView: [myTouch view]];
location = [[CCDirector sharedDirector]convertToGL:location];

CCSprite *circleSprite = (CCSprite*)[self getChildByTag:30];
//CGRect correctColorSprite1 = [circleSprite boundingBox];
CGRect correctColorSprite1 = CGRectMake(
    circleSprite.position.x - (circleSprite.contentSize.width/2), 
    circleSprite.position.y - (circleSprite.contentSize.height/2), 
    circleSprite.contentSize.width, 
    circleSprite.contentSize.height);

//find the center of the bounding box
CGPoint center=ccp(correctColorSprite1.size.width/2,correctColorSprite1.size.height/2);

//now test against the distance of the touchpoint from the center
   if (ccpDistance(center, location)<50) 
   {
     NSLog(@"inside");

   }
}
-(void)cctouchsbegind:(NSSet*)接触事件:(UIEvent*)事件
{
CGSize winSize=[[CCDirector sharedDirector]winSize];
UITouch*myTouch=[触摸任何对象];
CGPoint位置=[myTouch位置查看:[myTouch视图]];
location=[[CCDirector sharedDirector]convertToGL:location];
CCSprite*circleSprite=(CCSprite*)[self-getChildByTag:30];
//CGRect correctColorSprite1=[circleSprite boundingBox];
CGRect correctColorSprite1=CGRectMake(
circleSprite.position.x-(circleSprite.contentSize.width/2),
circleSprite.position.y-(circleSprite.contentSize.height/2),
圆圈prite.contentSize.width,
圆形(圆形、圆形、圆形、圆形、圆形、圆形、圆形、圆形、圆形、圆形、圆形、圆形、圆形、圆形、圆形、圆形、圆形、圆形、圆形、圆形、圆形、圆形、圆形、圆形、圆形、圆形、圆形、圆形、圆形、圆形、;
//找到边界框的中心
CGPoint center=ccp(correctColorSprite1.size.width/2,correctColorSprite1.size.height/2);
//现在根据接触点与中心的距离进行测试

if(ccpDistance(center,location)hi感谢您的帮助,当我尝试该代码时,触摸检测不到精灵本身。我离精灵更远。我的精灵被放置在'circle1=[CCSprite spriteWithFile:@“BB.png”];circle1.tag=30;circle1.position=ccp(200160);[self addChild:circle1](circleSprite.position,location)<50);
然后移除CGRect,我就可以工作了。感谢您的帮助@Saikat