как обнаружить прикосновение по кругу - PullRequest
0 голосов
/ 08 октября 2011

Я действительно помогу.Я немного запутался.у меня есть спрайт с кругом, и этот код

-(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");

}

, поскольку я знаю, что есть ограничивающая рамка, когда я слегка касаюсь за пределами верхнего круга, он все равно обнаруживает касание.

iЯ читал на некоторых форумах, что мне нужно определить расстояние до центра спрайта и точки касания.Но я действительно не знаю, как написать этот код.Мой размер круга составляет около 50 баллов.

Я надеюсь, что кто-то может мне помочь, дайте мне несколько фрагментов улучшенного кода, чтобы обнаружить касание только по кругу.Не с ограничительной рамкой.Ваша помощь очень велика.

1 Ответ

1 голос
/ 08 октября 2011

Попробуйте это:

-(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");

   }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...