0

draw在 cocos2d 中创建了一个带有函数的圆圈,我正在尝试检测圆圈线上的触摸点,假设用户触摸我要打印的圆圈底部 270,如果用户触摸我要打印的圆圈顶部 90 等等。 ..

我看过这个问题,但他们首先检测到一个精灵,然后比较是否在圈内或圈外触摸

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);
}

如何检测圆线上的触摸点?

4

3 回答 3

0

您可以添加ccTouchBegan/Moved/Ended到您的自定义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");
}

然后处理touch它对a的解释CCSprite(即计算触摸与the之间的距离center并确定触摸是否属于圆)。

于 2013-01-29T17:30:56.277 回答
0

如果您知道中心点和触摸位置,您应该知道它在圆上的位置所需的一切。

我假设您使用的是如何检测圆圈的代码

如果触摸在圆内,则取圆的中心点和触摸点,然后比较两者以查看 X 和 Y 的偏移量。根据 X/Y 偏移,您应该能够确定单击了圆的哪个部分(顶部、左侧、右侧、底部等)。如果你想要角度,找到两点的斜率。

于 2013-01-29T18:51:03.010 回答
0

试试这个,没有测试代码,根据你的需要改进它

-(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;
}
于 2013-02-06T18:28:43.227 回答