检测圆圈上的接触点,Cocos2d [英] Detect touch point on circle , Cocos2d
问题描述
我在cocos2d中创建了一个带有 draw
函数的圆圈,我想在圆线上检测触摸点,让我们说用户触摸我要打印的圆圈底部270,如果用户触摸圈子的顶部我想打印90等....
我看过这个问题,但他们检测到一个sprite,然后只是比较,如果touch内圈或外圈
http://www.cocos2d-iphone.org/forum/topic/21629
- (void)draw
{
CGSize winSize = [[CCDirector sharedDirector] winSize];
glLineWidth(10.0f);
ccDrawColor4F(0.2f,0.9f,0.02f,0.6f);
CGPoint center = ccp(winSize.width * 0.88,winSize.height * 0.8);
CGFloat radius = 100.f;
CGFloat angle = 0.f;
NSInteger segments = 100;
BOOL drawLineToCenter = YES;
ccDrawCircle(center,radius,angle,segments,drawLineToCenter);
}
如何检测圆线上的接触点?
尝试这个,没有测试代码,根据你的需要改进
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
CGSize winSize = [[CCDirector sharedDirector] winSize] ;
CGPoint location = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]];
CGPoint center = ccp(winSize.width * 0.88,winSize.height * 0.8);
//现在测试接触点距离中心的距离根据需要改变值
if(ccpDistance(center,location)< 100)
{
NSLog(@inside);
//计算弧度和圆度
CGPoint diff = ccpSub(center,location); //返回ccp(v1.x - v2.x,v1.y - v2.y );
float rads = atan2f(diff.y,diff.x);
float degs = -CC_RADIANS_TO_DEGREES(rads);
switch(degs)
{
case -90:
//这是底部
break;
case 90:
//这是top
break;
case 0:
//这是左侧
break;
case 180:
//这是右侧
break;
默认值:
break;
}
}
return YES;
}
I have created a circle with draw
function in cocos2d, I am trying to detect touch point on circle line, lets say user touches bottom of the circle I want to print 270, if user touches top of the circle I want to print 90 etc....
I have looked this questions but they detect a sprite first then just compare if touches inside or outside of the circle
http://www.cocos2d-iphone.org/forum/topic/21629
how to detect touch in a circle
- (void) draw
{
CGSize winSize = [[CCDirector sharedDirector] winSize];
glLineWidth(10.0f);
ccDrawColor4F(0.2f, 0.9f, 0.02f, 0.6f);
CGPoint center = ccp(winSize.width*0.88, winSize.height*0.8);
CGFloat radius = 100.f;
CGFloat angle = 0.f;
NSInteger segments = 100;
BOOL drawLineToCenter = YES;
ccDrawCircle(center, radius, angle, segments, drawLineToCenter);
}
How can I detect a touch point on the circle line?
Try this , didnt test the code , improved it according to your needs
-(BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent*)event
{
CGSize winSize = [[CCDirector sharedDirector] winSize];
CGPoint location = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]];
CGPoint center=ccp(winSize.width*0.88, winSize.height*0.8);
//now test against the distance of the touchpoint from the center change the value acording to your need
if (ccpDistance(center, location)<100)
{
NSLog(@"inside");
//calculate radians and degrees in circle
CGPoint diff = ccpSub(center, location);//return ccp(v1.x - v2.x, v1.y - v2.y);
float rads = atan2f( diff.y, diff.x);
float degs = -CC_RADIANS_TO_DEGREES(rads);
switch (degs)
{
case -90:
//this is bottom
break;
case 90:
//this is top
break;
case 0:
//this is left side
break;
case 180:
//this is right side
break;
default:
break;
}
}
return YES;
}
这篇关于检测圆圈上的接触点,Cocos2d的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!