2011-11-10 88 views
2

我想在科科斯2d手指畫線。科科斯2d畫線不工作

-(void) ccTouchesMoved:(NSSet *)inappropriateTouches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [inappropriateTouches anyObject]; 

    CGPoint currentTouchArea = [touch locationInView:[touch view] ]; 
    CGPoint lastTouchArea = [touch previousLocationInView:[touch view]]; 

    currentTouchArea = [[CCDirector sharedDirector] convertToGL:currentTouchArea]; 
    lastTouchArea = [[CCDirector sharedDirector] convertToGL:lastTouchArea]; 

    // throw to console my inappropriate touches 
    NSLog(@"current x=%2f,y=%2f",currentTouchArea.x, currentTouchArea.y); 
    NSLog(@"last x=%2f,y=%2f",lastTouchArea.x, lastTouchArea.y); 

    glColor4f(0.8, 1.0, 0.76, 1.0); 
    glLineWidth(6.0f); 
    ccDrawLine(currentTouchArea, lastTouchArea); 
} 

我使用此代碼,但沒有任何東西在屏幕上繪製。我的代碼有什麼問題?

回答

4

所有你想要在繪製方法中做的OpenGL繪圖。就像這樣:

-(void)draw 
{ 
    if(lastTouchArea != 0) 
    { 
     glColor4f(0.8, 1.0, 0.76, 1.0); 
     glLineWidth(6.0f); 
     ccDrawLine(currentTouchArea, lastTouchArea); 
     lastTouchArea = 0; 
    } 
} 
+0

它的工作原理,但爲什麼當繪製新舊的一行被刪除? –

+6

因爲每次刷新它都會清除屏幕。要堅持這些線條,您將不得不存儲一組點以表示您希望繪製的線條。 –

2

試試這個:保存該行以NSMutableArray的

-(void) ccTouchesMoved:(NSSet *)inappropriateTouches withEvent:(UIEvent *)event 
{ 
    UITouch *touchMyMinge = [inappropriateTouches anyObject]; 

    CGPoint currentTouchArea = [touchMyMinge locationInView:[touchMyminge view] ]; 
    CGPoint lastTouchArea = [touchMyMinge previousLocationInView:[touchMyMinge view]]; 

    // flip belly up. no one likes being entered from behind. 
    currentTouchArea = [[CCDirector sharedDirector] convertToGL:currentTouchArea]; 
    lastTouchArea = [[CCDirector sharedDirector] convertToGL:lastTouchArea]; 

    // throw to console my inappropriate touches 
    NSLog(@"current x=%2f,y=%2f",currentTouchArea.x, currentTouchArea.y); 
    NSLog(@"last x=%2f,y=%2f",lastTouchArea.x, lastTouchArea.y); 

    // add my touches to the naughty touch array 
    naughtyTouchArray addObject:NSStringFromCGPoint(currentTouchArea)]; 
    naughtyTouchArray addObject:NSStringFromCGPoint(lastTouchArea)]; 
} 

@implementation DrawMyTouch

-(id) init 
{ 
    if((self=[super init])) 
    { } 
    return self; 
} 

-(void)draw 
{ 
    glEnable(GL_LINE_SMOOTH); 

    for(int i = 0; i < [naughtyTouchArray count]; i+=2) 
    { 
     start = CGPointFromString([naughtyTouchArray objectAtIndex:i]); 
     end = CGPointFromString([naughtyTouchArray objectAtIndex:i+1]); 

     ccDrawLine(start, end); 
    } 
} 


-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    DrawMyTouch *line = [DrawMyTouch node]; 
    [self addChild: line]; 
} 

希望這有助於