ccDrawline直到触摸位置cocos2d的一半

时间:2013-10-28 10:00:46

标签: ios cocos2d-iphone

我的问题是我想画一条恰好是触摸位置一半的线 即,我在cctouchesmoved画一条线,它从第一个位置到我的触摸位置绘制一条线,但我的问题是我需要显示直到触摸位置的一半的线 这是我的代码

-(void)draw{
    glEnable(GL_LINE_SMOOTH);
    glLineWidth(3.0f); // set line width       
    glColor4f(0.8, 1.0, 0.76, 1.0);  // set line color.      
    ccDrawLine(point1,Point2);    
}

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{    
    UITouch* touch = [touches anyObject];
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    point1 = location;
    Point2=CGPointMake(size.width/2, size.height/2);
}

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

给出两个点a和b,其中a是'origin':

ccpMidPoint(a,b);

或者,更一般地说,你可以通过从b中减去a乘以所需的因子,然后重新添加:

来获得沿线的任意距离
float percentageOfDistanceAlongLine = 0.5f;
CGPoint pointAlongLine = ccpMult( ccpSub(b, a), percentageOfDistanceAlongLine);
pointAlongLine = ccpAdd(a, pointAlongLine);

所以,在你的情况下,point2 = a,point1 = b