如何检测圆圈中的触摸 [英] how to detect touch in a circle

查看:128
本文介绍了如何检测圆圈中的触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I really assistance. Im a little confused. i have a circle sprite, and this code

-(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分。

i have read in some forums that i need to detect distance of the centre of the sprite and the touch point. But i really don't know how to write that code. My circle size is around 50 points.

我希望有人可以帮助我给我一些改进的代码片段,以检测只在圆圈中的触摸。不与边界框。

I hope someone can help me out give me some snippets of a improved code to detect the touch only in the circle. Not with the bounding box. Your help is very great full.

推荐答案

请尝试以下操作:

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

   }
}

这篇关于如何检测圆圈中的触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆