问题
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?
回答1:
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;
}
回答2:
If you know the center point and the touch position you should have everything you need to know where it is on the circle.
I'm assuming your using the code from how to detect a circle
If the touch was inside the circle, then take the center point of the circle and the touch point then compare the two to see what the offset X and Y are. Based on the X/Y offset, you should be able to figure out which part of the circle (top, left, right, bottom, etc) was clicked. If you want the angle, find the slope of the two points.
回答3:
You could add ccTouchBegan/Moved/Ended
to your custom CCNode
:::
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
NSLog(@"ccTouchBegan Called");
if ( ![self containsTouchLocation:touch] ) return NO;
NSLog(@"ccTouchBegan returns YES");
return YES;
}
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchPoint = [touch locationInView:[touch view]];
touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];
NSLog(@"ccTouch Moved is called");
}
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
NSLog(@"ccTouchEnded is called");
}
and then handle the touch
like it was explained for a CCSprite
(i.e., calculating the distance between the touch and the center
and deciding whether the touch belongs to the circle).
来源:https://stackoverflow.com/questions/14586876/detect-touch-point-on-circle-cocos2d